类型错误:从文件中拆分行时,需要类似字节的对象.gz而不是'str'对象



我有一个gz文件sample.gz.

This is first line of sample gz file.
This is second line of sample gz file.

我读了这个。gz文件,然后逐行拆分它。一旦我有了单独的行,我就用空格作为分隔符进一步把它分成几部分。

import gzip
logfile = "sample.gz"
with gzip.open(logfile) as page:
for line in page:
string = line.split(" ")
print(*string, sep = ',')

我期望输出像

This,is,first,line,of,sample,gz,file.
This,is,second,line,of,sample,gz,file.

但不是上面的结果,我收到TypeError:

TypeError:需要一个bytes-like object,而不是'str'

为什么拆分函数不工作,因为它应该?

默认情况下,gzip.open以二进制方式打开文件。这意味着读取返回bytes对象,并且bytes对象只能在其他bytes对象上分割,而不能在字符串上分割。

如果你想要字符串,使用modeencoding参数gzip.open:

with gzip.open(logfile, 'rt', encoding='utf-8') as page:
...

如果你们看到上面的评论,有几个方法可以使用。我跟随Python从gzip文件中读取csv行Mkrieger1,并提出了以下解决方案。

import gzip
logfile = "sample.gz"
with gzip.open(logfile) as page:
for line in page:
string = line.decode('utf-8').split(' ')
print(*string, sep = ',')

感谢您的快速回复。

最新更新