扩展名为 .vcf (VCARD) 的文件片段
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;=D0=9C=D0=A2=D0=A1;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=D0=9C=D0=A2=D0=A1
TEL;CELL;PREF:+78002500123
END:VCARD
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;=D0=A1=D0=BB=D1=83=D0=B6=D0=B1=D0=B0=20=D1=81=D0=BF=D0=B0=D1=81=D0=B5=
=D0=BD=D0=B8=D1=8F;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=D0=A1=D0=BB=D1=83=D0=B6=D0=B1=D0=B0=20=D1=81=D0=BF=D0=B0=D1=81=D0=B5=
=D0=BD=D0=B8=D1=8F
TEL;CELL;PREF:112
END:VCARD
我的代码
import vobject
with open('test.vcf') as source_file:
vcf_read_components = vobject.readComponents(source_file)
for item in vcf_read_components:
print(item)
出现解析错误
Traceback (most recent call last):
File "/home/xxx/experiments/book.py", line 5, in <module>
for item in vcf_read_components:
File "/home/xxx/experiments/venv/lib/python3.11/site-packages/vobject/base.py", line 1166, in readComponents
vline = textLineToContentLine(line, n)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/xxx/experiments/venv/lib/python3.11/site-packages/vobject/base.py", line 984, in textLineToContentLine
return ContentLine(*parseLine(text, n), **{"encoded": True, "lineNumber": n})
^^^^^^^^^^^^^^^^^^
File "/home/xxx/experiments/venv/lib/python3.11/site-packages/vobject/base.py", line 864, in parseLine
raise ParseError("Failed to parse line: {0!s}".format(line), lineNumber)
vobject.base.ParseError: At line 19: Failed to parse line: =D0=BD=D0=B8=D1=8F;;;
<VCARD| [<VERSION{}2.1>, <FN{'CHARSET': ['UTF-8']}МТС>, <N{'CHARSET': ['UTF-8']} МТС >, <TEL{}+78002500123>]>
从 2024 年 11 月 15 日起,当联系人拥有多个号码时进行澄清。
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=D0=94=D0=BE=D1=80=D1=84;=D0=9C=D0=B0=D0=BA=D1=81;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=D0=9C=D0=B0=D0=BA=D1=81=20=D0=94=D0=BE=D1=80=D1=84
TEL;CELL:+79128051234
TEL;CELL:+79097484321
END:VCARD
import vobject
with open('test.vcf') as source_file:
vcf_read_components = vobject.readComponents(source_file, allowQP=True)
for item in vcf_read_components:
print(item)
print(item.fn.value)
print(item.tel.value)
item.tel.value
仅返回第一个
<VCARD| [<VERSION{}2.1>, <FN{'CHARSET': ['UTF-8']}Макс Дорф>, <N{'CHARSET': ['UTF-8']} Макс Дорф >, <TEL{}+79128051234>, <TEL{}+79097484321>]>
Макс Дорф
+79128051234
=D0=A1=D0=BB=D1=83=D0=B6=D0=B1=D0=B0=20=D1=81=D0=BF=D0=B0=D1=81=D0=B5 ->= = <-解析器在这里死掉 D0=BD=D0=B8=D1=8F;;;
为了解决这个问题,通常启用
quoted-printable
数据处理就足够了:我考虑了这些建议(再次感谢大家!)并添加了一些。这是带注释的代码:
结果: