如何按行获取gz包中的数据???
这个文件是一个压缩的txt文档,里面有很多行数据GZ_FILE_PATH
这个是解压函数的定义
def read_gz_file(path):
if os.path.exists(path):
with gzip.open(path, 'rt') as pf:
return pf.read()
else:
print('the path [{}] is not exist!'.format(path))
在这里是主函数的调用,如果是这么写,会输出txt全部的内容,有好多行
con = read_gz_file(GZ_FILE_PATH)
print(con)
我想按行获取,进行一些处理
可是写成下面这样之后
for line in con:
print(line)
输出的是一个个的字符,而且好像只有一行的字符
该怎么获得一行行的啊 回复 1# taxuelingbing
你可以:for line in con.split():
print line
页:
[1]