Python - 如何使用NUL分隔行读取文件?

use*_*812 10 python nul

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

f = open('./my.csv', 'r')
for line in f:
    print line
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的建议.

Mar*_*ers 14

如果您的文件足够小,您可以将其全部读入内存,则可以使用split:

for line in f.read().split('\0'):
    print line
Run Code Online (Sandbox Code Playgroud)

否则,您可能希望从有关此功能请求的讨论中尝试此配方:

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
Run Code Online (Sandbox Code Playgroud)

我还注意到你的文件有一个"csv"扩展名.Python内置了一个CSV模块(import csv).有一个属性,Dialect.lineterminator但它目前没有在阅读器中实现:

Dialect.lineterminator

用于终止writer生成的行的字符串.它默认为'\ r \n'.

注意阅读器是硬编码的,可识别'\ r'或'\n'作为行尾,并忽略行终止符.此行为将来可能会发生变化.