python 用块读取文件,但以换行符结束(\n)

发布于 2025-01-04 00:36:07 字数 238 浏览 2 评论 0原文

test.txt 是一个“\n”分割文本文件:

f = open('test.txt','r') f.read(256)

但是读取256时,最后一条记录可能不是整行。

如何读取如:

我设置读取256 但是当 248 是“\n”时 256 最后的记录没有整行 只需读取 248,f.tell() 给出 248 的位置。

谢谢。

test.txt is a "\n" split text file:

f = open('test.txt','r') f.read(256)

But while read 256, the last records may not with full line.

How to read such as:

I set read 256
but when 248 is the "\n"
and 256 the last records not with full line
just read 248, and f.tell() give the 248 position.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

我早已燃尽 2025-01-11 00:36:07

如果您使用换行符来分割数据,为什么不以同样的方式读取它呢?

with open('test.txt', 'r') as f:
    lines = f.readlines()
# Now each line in lines is a complete line.

If you're using newlines to split your data, why not read it in the same way?

with open('test.txt', 'r') as f:
    lines = f.readlines()
# Now each line in lines is a complete line.
萧瑟寒风 2025-01-11 00:36:07

您要做的就是阅读完整的行。对于此任务,您通常会执行一些具有此效果的操作。

size_so_far = 0
contents = []

for line in open('test.txt'):
      size_so_far += len(line)
      if size_so_far > 256:
         break
      contents.append(line)

contents = "".join(contents)

What you want to do is read complete lines. For this task, you usually do something of this effect.

size_so_far = 0
contents = []

for line in open('test.txt'):
      size_so_far += len(line)
      if size_so_far > 256:
         break
      contents.append(line)

contents = "".join(contents)
时间你老了 2025-01-11 00:36:07

读取由 '\n''\r''\r\n'< 分隔的可变长度行的文件的最简单方法/code> 或者甚至是这些的混合:

with open('yourfile.txt', 'rU') as f:
    for line in f:
        do_something_with(f)
        # optional, if you want to bale out after 256 bytes:
        if f.tell() >= 256: break

它的作用是读取大块,找到行结尾,并一次生成一行。底层代码是用 C 编写的。我还没有看到任何证据表明用 Python 代码做同样的事情会更快。

The simplest way to read a file with variable-length lines separated by any of '\n', '\r' or '\r\n' or even a mixture of those is:

with open('yourfile.txt', 'rU') as f:
    for line in f:
        do_something_with(f)
        # optional, if you want to bale out after 256 bytes:
        if f.tell() >= 256: break

What that does is read large blocks, find the line endings, and yield a line at a time. The underlying code is written in C. I have yet to see any evidence that doing the same thing in Python code would be faster.

小姐丶请自重 2025-01-11 00:36:07

你关心效率吗?

这是一种方法:

data=f.read(256)
data=data.splitlines(True)
if data[-1]!=data[-1].splitlines()[-1]:
    #must be newline at end of last line
    data="".join(data)
else:
    data="".join(data[:-1])

print data

do you care about efficiency?

here is one way to do it:

data=f.read(256)
data=data.splitlines(True)
if data[-1]!=data[-1].splitlines()[-1]:
    #must be newline at end of last line
    data="".join(data)
else:
    data="".join(data[:-1])

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