gpt4 book ai didi

Python - 如何读取带有 NUL 分隔行的文件?

转载 作者:太空狗 更新时间:2023-10-29 18:25:53 25 4
gpt4 key购买 nike

我通常使用以下 Python 代码从文件中读取行:

f = open('./my.csv', 'r')
for line in f:
print line

但是如果文件是由“\0”(而不是“\n”)分隔的行呢?是否有可以处理此问题的 Python 模块?

感谢您的任何建议。

最佳答案

如果你的文件足够小,你可以将它全部读入内存,你可以使用拆分:

for line in f.read().split('\0'):
print line

否则你可能想从关于这个 feature request 的讨论中尝试这个食谱:

def fileLineIter(inputFile,
inputNewline="\n",
outputNewline=None,
readSize=8192):
"""Like the normal file iter but you can set what string indicates newline.

The newline string can be arbitrarily long; it need not be restricted to a
single character. You can also set the read size and control whether or not
the newline string is left on the end of the iterated lines. Setting
newline to '\0' is particularly good for use with an input file created with
something like "os.popen('find -print0')".
"""
if outputNewline is None: outputNewline = inputNewline
partialLine = ''
while True:
charsJustRead = inputFile.read(readSize)
if not charsJustRead: break
partialLine += charsJustRead
lines = partialLine.split(inputNewline)
partialLine = lines.pop()
for line in lines: yield line + outputNewline
if partialLine: yield partialLine

我还注意到您的文件有一个“csv”扩展名。 Python 中内置了一个 CSV 模块(导入 csv)。有一个名为 Dialect.lineterminator 的属性然而,它目前还没有在阅读器中实现:

Dialect.lineterminator

The string used to terminate lines produced by the writer. It defaults to '\r\n'.

Note The reader is hard-coded to recognise either '\r' or '\n' as end-of-line, and ignores lineterminator. This behavior may change in the future.

关于Python - 如何读取带有 NUL 分隔行的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com