用换行符的指定分隔符读取文件 [英] Reading a file with a specified delimiter for newline
问题描述
我有一个文件,其中的行使用分隔符分隔。
。我想逐行读取这个文件,其中行应该基于。
而不是换行符。
其中一种方法是:
$ p $ f = open('file' ()。'):
#....做一些工作
f.close()
但是如果我的文件太大,这不是有效的。
$ b
open
支持一个参数'newline '但是这个参数只需要输入 None,'','\ n','\r'和'\r\\\
a href =http://docs.python.org/2/library/io.html =noreferrer> here 。
'
<有没有什么方法可以有效地读取文件行,但基于预先指定的分隔符?
可以使用一个生成器:
def myreadlines(f,newline):
buf =
while True:
,而buf中的换行符:
pos = buf.index(换行符)
yield buf [:pos]
buf = buf [pos + len(newline):]
chunk = f.read(4096)
如果不是chunk:
yield buf
break
buf + = chunk
with open(' (f,。):
print line
I have a file in which lines are separated using a delimeter say .
. I want to read this file line by line, where lines should be based on presence of .
instead of newline.
One way is:
f = open('file','r')
for line in f.read().strip().split('.'):
#....do some work
f.close()
But this is not memory efficient if my file is too large. Instead of reading a whole file together I want to read it line by line.
open
supports a parameter 'newline' but this parameter only takes None, '', '\n', '\r', and '\r\n'
as input as mentioned here.
Is there any way to read files line efficiently but based on a pre-specified delimiter?
You could use a generator:
def myreadlines(f, newline):
buf = ""
while True:
while newline in buf:
pos = buf.index(newline)
yield buf[:pos]
buf = buf[pos + len(newline):]
chunk = f.read(4096)
if not chunk:
yield buf
break
buf += chunk
with open('file') as f:
for line in myreadlines(f, "."):
print line
这篇关于用换行符的指定分隔符读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!