带有 TextIOWrapper 的 python zipfile 模块
我编写了以下代码来读取压缩目录内的文本文件。由于我不需要以字节为单位的输出,因此我添加了 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...
谢谢!
I wrote the following piece of code to read a text file inside of a zipped directory. Since I don't want the output in bytes I added the TextIOWrapper to display the output as a string. Assuming that this is the right way to read a zip file line by line (if it isn't let me know), then why does the output print a blank line? Is there any way to get rid of it?
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...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
zipfile.open
以二进制模式打开压缩文件,它不会去掉回车符(即“\r”),我的TextIOWrapper
的默认值也没有。测试。尝试配置TextIOWrapper
以使用通用换行符(即newline=None
):输出:
在 Python 中按行迭代文件时的正常行为是在末尾保留换行符。
print
函数还会添加换行符,因此您将得到一个空行。要仅打印文件,您可以使用print(words.read())
。或者您可以使用打印函数的end
选项:print(line, end='')
。zipfile.open
opens the zipped file in binary mode, which doesn't strip out carriage returns (i.e. '\r'), and neither did the defaults forTextIOWrapper
in my test. Try configuringTextIOWrapper
to use universal newlines (i.e.newline=None
):Output:
The normal behavior when iterating a file by line in Python is to retain the newline at the end. The
print
function also adds a newline, so you'll get a blank line. To just print the file you could instead useprint(words.read())
. Or you could use theend
option of the print function:print(line, end='')
.