python csv生成vcf
上一篇說vcf轉(zhuǎn)換成csv
這篇就說vcf轉(zhuǎn)csv,這次就轉(zhuǎn)回去
其實最適合csv轉(zhuǎn)vcf,還是用VBA,我找到了一個大神寫的,沒密碼,而且界面調(diào)真漂亮,可惜vcf的版本好像不對。
個人又想練習(xí)一下python,就這樣折騰下來了,不好看,也不好用,唉
導(dǎo)入的csv庫,用的類,看不懂,最后還是序列的方法,后面加個\n,當(dāng)回車了
加個空值判斷,算是0.3版本
想做成類似于csv那樣的庫,估計要自己寫讀取
可以很方便的添加新的聯(lián)系人,就三行,姓名,備注,加電話號碼
新公司的聯(lián)系方式就用這個導(dǎo)進去了,公司+名字,備注寫職位,通信錄好看多了
個人用的小米6,導(dǎo)入成功后,看生成vcf文件簡單,讓同事的mate10也試了一下,居然也行
==================================================================
翻了一下,貌似是文件讀取和字符串切片,line.read和split()這兩個函數(shù)
在窗口試了一下,大概就是
f=open(“00002.csv”).read().split("\n")
好像比csv.read()簡單
===================================================================
import osdef csv2vcf(file):rf = open(file).read().split("\n")name = file_path_shortname_extension(file)with open(name[0]+name[1]+".vcf", "w", encoding="utf-8") as wf:content = ["BEGIN:VCARD", "VERSION:3.0","","","","END:VCARD\n\n", ]for line in rf:title = line.split(",")if title[0] == "名字":continueif title[0] == "":breakif not title[0] == "": content[2] = "FN:"+title[0]if not title[1] == "": content[3] = "ORG:" + title[1]if not title[2] == "": content[4] = "TEL;TYPE=CELL:" + title[2]str="\n".join(content)wf.write(str)content = ["BEGIN:VCARD", "VERSION:3.0","","","","END:VCARD\n\n", ]print("寫入完成")def vcf2csv(filename)name = file_path_shortname_extension(file)with open(filename, 'r', encoding='utf-8') as rf, open(name[0]+name[1]+".csv", 'w') as wf:content = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']tel = 4wf.write("名字,備注,家庭電話,工作電話,其他電話,\n")for line in rf.readlines():if line.startswith('FN:'):content[0] = line[3:].strip()elif line.startswith("ORG;CHARSET=UTF-8"):content[1] = line[18:].strip()elif line.startswith("TEL;TYPE=HOME"):content[2] = line[14:].strip()elif line.startswith("TEL;TYPE=WORK"):content[3] = line[14:].strip()elif line.startswith('TEL'):if tel > 15:continuepos = line.find(':')content[tel] = line[pos + 1:].strip()tel = tel + 1elif line.startswith('END'):str = ','.join(content) + '\n'wf.write(str)content = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','', '']tel = 4def file_path_shortname_extension(file):#返回文件拓展名.csv 等(filepath,temp_filename) = os.path.split(file)(shortname,extension) = os.path.splitext(temp_filename)return (filepath,shortname,extension)def cv(file):if not os.path.isfile(file) :print("文件不存在")else:a = file_path_shortname_extension(file)[2]if a == ".csv" :print("此文件為csv文件")csv2vcf(file)print("已生成vcf文件")elif a == ".vcf":print ("此文件為vcf文件")vcf2csv(file)print("已生成csv文件")else :print("請選擇正確的csv文件或者vsf文件")加了點料
總結(jié)
以上是生活随笔為你收集整理的python csv生成vcf的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 找不到驱动器。名为“.C”的驱动器不存在
- 下一篇: 服务器导购:选购服务器应看哪些配置参数