带有 TextIOWrapper 的 python zipfile 模块

发布于 2024-12-11 10:50:55 字数 620 浏览 0 评论 0原文

我编写了以下代码来读取压缩目录内的文本文件。由于我不需要以字节为单位的输出,因此我添加了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-12-18 10:50:55

zipfile.open 以二进制模式打开压缩文件,它不会去掉回车符(即“\r”),我的 TextIOWrapper 的默认值也没有。测试。尝试配置 TextIOWrapper 以使用通用换行符(即 newline=None):

import zipfile
import io

zf = zipfile.ZipFile('data/test_zip.zip')
for filename in zf.namelist():
    with zf.open(filename, 'r') as f:
        words = io.TextIOWrapper(f, newline=None)
        for line in words:
            print(repr(line))

输出:

'This is a test line...\n'
'This is a test line...'

在 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 for TextIOWrapper in my test. Try configuring TextIOWrapper to use universal newlines (i.e. newline=None):

import zipfile
import io

zf = zipfile.ZipFile('data/test_zip.zip')
for filename in zf.namelist():
    with zf.open(filename, 'r') as f:
        words = io.TextIOWrapper(f, newline=None)
        for line in words:
            print(repr(line))

Output:

'This is a test line...\n'
'This is a test line...'

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 use print(words.read()). Or you could use the end option of the print function: print(line, end='').

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文