使用fileinput读取特定行 [英] use fileinput to read a specific line

查看:71
本文介绍了使用fileinput读取特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧

我是python的新手

i需要从头文件中读取第4行

使用linecache会使我的电脑崩溃到期内存加载,因为

i正在处理2000个文件,每个是8mb

fileinput首先不要将文件加载到内存中

如何使用fileinput模块从文件中读取特定行?


for fileinput.Fileinput(''sample.txt'')中的行

????

hi everybody
im a newbie in python
i need to read line 4 from a header file
using linecache will crash my computer due to memory loading, because
i am working on 2000 files each is 8mb

fileinput don''t load the file into memory first
how do i use fileinput module to read a specific line from a file?

for line in fileinput.Fileinput(''sample.txt'')
????

推荐答案

1月7日晚上7:15,jo3c< JO3chi ... @ gmail。编写:
On Jan 7, 7:15 pm, jo3c <JO3chi...@gmail.comwrote:

hi everybody

我是python的新手

i需要从头文件中读取第4行
使用linecache的
会因为内存加载而导致计算机崩溃,因为

i正在处理2000个文件,每个是8mb

fileinput don首先将文件加载到内存中

如何使用fileinput模块读取特定的lin来自文件的文件?


for fileinput.Fileinput(''sample.txt'')中的行

????
hi everybody
im a newbie in python
i need to read line 4 from a header file
using linecache will crash my computer due to memory loading, because
i am working on 2000 files each is 8mb

fileinput don''t load the file into memory first
how do i use fileinput module to read a specific line from a file?

for line in fileinput.Fileinput(''sample.txt'')
????



假设它是一个文本文件,你可以使用这样的东西:


lnum = 0#行号


for line in file(" sample.txt"):

lnum + = 1

if lnum> = 4 :break


变量line如果我没有弄错的话,应该以第4行的内容结束。要处理多个文件,只需将这些代码包装成:


for file0 in files:


lnum = 0#行号

for line in file(file0):

lnum + = 1

if lnum> = 4:break


#用line做某事


其中files是一个要读取的文件列表。


未经测试。

Assuming it''s a text file, you could use something like this:

lnum = 0 # line number

for line in file("sample.txt"):
lnum += 1
if lnum >= 4: break

The variable "line" should end up with the contents of line 4 if I am
not mistaken. To handle multiple files, just wrap that code like this:

for file0 in files:

lnum = 0 # line number

for line in file(file0):
lnum += 1
if lnum >= 4: break

# do something with "line"

where "files" is a list of the files to be read.

That''s not tested.



鉴于OP正在讨论要处理的2000个文件,我想我会建议明确的open()和close()调用,以避免有大量的I / O.

结构浮动...
Given that the OP is talking 2000 files to be processed, I think I''d
recommend explicit open() and close() calls to avoid having lots of I/O
structures floating around...



好​​点。我没有想到这一点。它也可以按如下方式完成:


for fileN in files:


lnum = 0#行号

input = file(fileN)


输入行:

lnum + = 1

如果lnum> = 4 :break


input.close()


#用line做点什么


我想,其中一个或六个中的六个。

Good point. I didn''t think of that. It could also be done as follows:

for fileN in files:

lnum = 0 # line number
input = file(fileN)

for line in input:
lnum += 1
if lnum >= 4: break

input.close()

# do something with "line"

Six of one or half a dozen of the other, I suppose.


1月7日晚上9:41,Dennis Lee Bieber< wlfr ... @ ix .netcom.comwrote:
On Jan 7, 9:41 pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:

2008年1月7日星期一20:10:58 -0800(PST),Russ P.

< Russ.Paie ... @ gmail.com在comp.lang.python中声明了以下内容:
On Mon, 7 Jan 2008 20:10:58 -0800 (PST), "Russ P."
<Russ.Paie...@gmail.comdeclaimed the following in comp.lang.python:

for file0 in files:
for file0 in files:


lnum = 0#行号
lnum = 0 # line number


for line in file(file0):

lnum + = 1

如果是lnum& gt; = 4:中断
for line in file(file0):
lnum += 1
if lnum >= 4: break


#使用line进行操作
# do something with "line"


where" files"是要读取的文件列表。
where "files" is a list of the files to be read.



鉴于OP正在讨论要处理的2000个文件,我想我会建议明确打开()并关闭(b) )要求避免让很多I / O

结构浮动......


fid in file_list:

fin = open(fid)

jnk = fin.readline()

jnk = fin.readline()

jnk = fin.readline()

ln = fin.readline()

fin.close()


是的,编写三个垃圾读取确实意味着维护将是一个痛苦

(我们现在需要第5行,而不是第4行 - 并且需要添加

另一个jnk =行)...我也许考虑用以下内容替换所有四个readline()



$ x $ b for crange in xrange(4):

ln = fin.readline ()


因为它不需要单独的行计数器/测试的开销和

将留下第四个输入在ln中的行退出。

-

Wulfraed Dennis Lee Bieber KD6MOG

wlfr ... @ ix.netcom.com wulfr ... @ bestiaria .com
HTTP://wlfraed.home.netcom.com/

(Bestiaria支持人员:web-a ... @ bestiaria.com)
HTTP://www.bestiaria.com/



一秒钟的想法,我想知道引用计数机制是否会

是聪明的足以在外循环的每个
迭代中自动关闭先前的文件。如果是这样,文件不需要明确关闭


One second thought, I wonder if the reference counting mechanism would
be "smart" enough to automatically close the previous file on each
iteration of the outer loop. If so, the files don''t need to be
explicitly closed.


这篇关于使用fileinput读取特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆