python读取txt文档判断某一个值是否为nul,Python-如何使用NUL分隔线读取文件?

I usually use the following Python code to read lines from a file :

f = open('./my.csv', 'r')

for line in f:

print line

But how about if the file is line delimited by "\0" (not "\n") ? Is there a Python module that could handle this ?

Thanks for any advice.

解决方案

If your file is small enough that you can read it all into memory you can use split:

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

print line

Otherwise you might want to try this recipe from the discussion about this 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

I also noticed your file has a "csv" extension. There is a CSV module built into Python (import csv). There is an attribute called Dialect.lineterminator however it is currently not implemented in the reader:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值