python csv生成vcf

上一篇说vcf转换成csv

这篇就说vcf转csv,这次就转回去

其实最适合csv转vcf,还是用VBA,我找到了一个大神写的,没密码,而且界面调真漂亮,可惜vcf的版本好像不对。
个人又想练习一下python,就这样折腾下来了,不好看,也不好用,唉

import csv
reader = csv.reader(open("00002.csv"))
with open("00002_2.vcf", "w", encoding="utf-8") as wf:
    content = ["BEGIN:VCARD\n", "VERSION:3.0\n","","","","END:VCARD\n\n", ]
    for title in reader:
        if not title[0] == "":content[2] = "FN:"+title[0]+"\n"
        if not title[1] == "":content[3] = "ORG:" + title[1] + "\n"
        if not title[2] == "":content[4] = "TEL;TYPE=CELL:" + title[2] + "\n"
        str="".join(content)
        wf.write(str)
        content = ["BEGIN:VCARD\n", "VERSION:3.0\n","","","","END:VCARD\n\n", ]

导入的csv库,用的类,看不懂,最后还是序列的方法,后面加个\n,当回车了

加个空值判断,算是0.3版本

想做成类似于csv那样的库,估计要自己写读取

可以很方便的添加新的联系人,就三行,姓名,备注,加电话号码

新公司的联系方式就用这个导进去了,公司+名字,备注写职位,通信录好看多了

个人用的小米6,导入成功后,看生成vcf文件简单,让同事的mate10也试了一下,居然也行

==================================================================

翻了一下,貌似是文件读取和字符串切片,line.read和split()这两个函数
在窗口试了一下,大概就是
f=open(“00002.csv”).read().split("\n")
好像比csv.read()简单

===================================================================

import os

def 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] == "名字":continue
            if title[0] == "":break
            if 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 = 4
        wf.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:
                    continue
                pos = line.find(':')
                content[tel] = line[pos + 1:].strip()
                tel = tel + 1
            elif line.startswith('END'):
                str = ','.join(content) + '\n'
                wf.write(str)
                content = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','', '']
                tel = 4


def 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文件")

加了点料

猜你喜欢

转载自blog.csdn.net/weixin_43221117/article/details/82749894