在python代码中使用ajax表单上载文件时vobject文件出错

2024-05-19 20:12:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我上传的vcf文件django的形式,并希望转换成vobject,但它会给出错误

当我尝试使用以下代码时,它将工作

file = open('test.vcf', 'r')

但在下面的代码中,它不起作用

file = request.FILES.get('vcf_file')
with file as f:
    vc = vobject.readComponents(str(f.read()).encode('utf-8'))
    vo = next(vc, None)
    while vo is not None:
        fname = vo.contents.get('fn')[0].value
        email = vo.contents.get('email')[0].value if vo.contents.get('email') else ''
        numbers = []
        if vo.contents.get('tel'):
            for v in vo.contents['tel']:
                no = v.value.replace("-", "")
                if no not in numbers and no[-10:] not in numbers[-10:]:
                    numbers.append(no)

        print(fname, ', ', numbers, ', ', email)
        vo = next(vc, None)

我期望像这样的输出

Pravin ,  ['+91971282123'] ,  
VH Patel ,  ['+918511123341'] ,  

但它会抛出以下错误:

    vo = next(vc, None)
  File "/home/vahta/vhcshop_env/lib/python3.5/site-packages/vobject/base.py", line 1101, in readComponents
    vline = textLineToContentLine(line, n)
  File "/home/vahta/vhcshop_env/lib/python3.5/site-packages/vobject/base.py", line 925, in textLineToContentLine
    return ContentLine(*parseLine(text, n), **{'encoded': True,
  File "/home/vahta/vhcshop_env/lib/python3.5/site-packages/vobject/base.py", line 813, in parseLine
    raise ParseError("Failed to parse line: {0!s}".format(line), lineNumber)
vobject.base.ParseError: At line 1: Failed to parse line: b'BEGIN:VCARD\r\nVERSION:3.0\r\nN:vdfn;Pravin;;;\r\nFN:Pravin vdfn\r\nTEL;TYPE=CELL:+919712823033\r\nTEL;TYPE=CELL:+919712823033\r\nEND:VCARD\r\n

Tags: noinnonebasegetemaillinecontents