How to open an unicode text file inside a zip?

后端 未结 3 1707
不知归路
不知归路 2020-12-19 06:04

I tried

with zipfile.ZipFile(\"5.csv.zip\", \"r\") as zfile:
    for name in zfile.namelist():
        with zfile.open(name, \'rU\') as readFile:
                    


        
3条回答
  •  天涯浪人
    2020-12-19 06:42

    The reason why you're seeing that error is because you are trying to mix bytes with unicode. The argument to split must also be byte-string:

    >>> line = b'$0.0\t1822\t1\t1\t1\n'
    >>> line.split(b'\t')
    [b'$0.0', b'1822', b'1', b'1', b'1\n']
    

    To get a unicode string, use decode:

    >>> line.decode('utf-8')
    '$0.0\t1822\t1\t1\t1\n'
    

提交回复
热议问题