【发布时间】:2011-12-13 00:30:55
【问题描述】:
我编写了以下代码来读取压缩目录中的文本文件。由于我不希望以字节为单位输出,因此我添加了 TextIOWrapper 以将输出显示为字符串。假设这是逐行读取 zip 文件的正确方法(如果没有让我知道),那么为什么输出会打印一个空行?有什么办法可以摆脱吗?
import zipfile
import io
def test():
zf = zipfile.ZipFile(r'C:\Users\test\Desktop\zip1.zip')
for filename in zf.namelist():
words = io.TextIOWrapper(zf.open(filename, 'r'))
for line in words:
print (line)
zf.close()
test()
>>>
This is a test line...
This is a test line...
>>>
The two lines in the file inside of the zipped folder are:
This is a test line...
This is a test line...
谢谢!
【问题讨论】:
标签: python string types zip readline