我正在用python处理一些vcard文件。我已经把vcard文件解析成字典了。现在我想在编辑之后将它保存到一个新的vcf文件中。我找不到任何解决方案。vcards中有没有解析python字典的库或模块?
person = {'n': 'Forrest Gump',
'fn': 'Forrest Gump', 'tel': '(111) 555-1212',
'email': 'forrestgump@example.com',
'photo': ';http://www.example.com/dir_photos/my_photo.gif',
'adr': 'Kathmandu, Nepal'}当我给这个字典的时候,我想得到一个vcf文件,如下所示。
BEGIN:VCARD
VERSION:3.0
N:Gump;Forrest;;Mr.;
FN:Forrest Gump
TITLE:Shrimp Man
PHOTO;VALUE=URI;TYPE=GIF:;http://www.example.com/dir_photos/my_photo.gif
EMAIL:forrestgump@example.com
END:VCARD发布于 2019-08-06 10:13:27
你可能应该看看这里的vobject http://eventable.github.io/vobject/
import vobject
person = {'n': 'Forrest Gump',
'fn': 'Forrest Gump', 'tel': '(111) 555-1212',
'email': 'forrestgump@example.com',
'photo': ';http://www.example.com/dir_photos/my_photo.gif',
'adr': 'Kathmandu, Nepal'}
vcard = vobject.readOne('\n'.join([f'{k}:{v}' for k, v in person.items()]))
vcard.name = 'VCARD'
vcard.useBegin = True
vcard.prettyPrint()
with open('test.vcf', 'w', newline='') as f:
f.write(vcard.serialize())请注意,在不设置.name和.useBegin的情况下写入卡将省略BEGIN和END,并且生成的文件将不是有效的vCard。我不确定是否有更方便的方法来使用这个库,但是您可以简单地创建您自己的类,这个类继承自现有的类(或者从新的类调用函数)来清理代码。
https://stackoverflow.com/questions/57367944
复制相似问题