Alo*_*hal 241
如果要读取的文件很大,并且您不想立即读取内存中的整个文件:
fp = open("file")
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break
fp.close()
Run Code Online (Sandbox Code Playgroud)
请注意,i == n-1对于第nth行.
在Python 2.6或更高版本中:
with open("file") as fp:
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break
Run Code Online (Sandbox Code Playgroud)
Ada*_*tan 144
快速回答:
f=open('filename')
lines=f.readlines()
print lines[25]
print lines[29]
Run Code Online (Sandbox Code Playgroud)
要么:
lines=[25, 29]
i=0
f=open('filename')
for line in f:
if i in lines:
print i
i+=1
Run Code Online (Sandbox Code Playgroud)
有一个更优雅的解决方案,用于提取许多行:linecache(由"python:如何跳转到一个巨大的文本文件中的特定行?",以前的stackoverflow.com问题).
引用上面链接的python文档:
>>> import linecache
>>> linecache.getline('/etc/passwd', 4)
'sys:x:3:3:sys:/dev:/bin/sh\n'
Run Code Online (Sandbox Code Playgroud)
更改4为您想要的行号,然后您就可以了.请注意,4会带来第五行,因为计数是从零开始的.
如果文件可能非常大,并且在读入内存时会导致问题,那么采用@Alok的建议并使用enumerate()可能是个好主意.
总结:
fileobject.readlines()或for line in fileobject作为小文件的快速解决方案.linecache更优雅的解决方案,可以快速读取多个文件.enumerate()可能非常大的文件,并且不适合内存.请注意,使用此方法可能会因为按顺序读取文件而变慢.Ale*_*lli 29
快速而紧凑的方法可能是:
def picklines(thefile, whatlines):
return [x for i, x in enumerate(thefile) if i in whatlines]
Run Code Online (Sandbox Code Playgroud)
这接受任何打开的类文件对象thefile(留给调用者是否应该从磁盘文件打开,或通过例如套接字,或其他类似文件的流)和一组从零开始的行索引whatlines,并返回一个列表,内存占用少,速度合理.如果要返回的行数很大,您可能更喜欢生成器:
def yieldlines(thefile, whatlines):
return (x for i, x in enumerate(thefile) if i in whatlines)
Run Code Online (Sandbox Code Playgroud)
这基本上只适用于循环 - 请注意,唯一的区别来自于在return语句中使用舍入而不是方括号,分别进行列表推导和生成器表达式.
此外应注意,尽管"行"和"文件"提到这些功能很多,很多更普遍的-他们会在工作的任何可迭代的,无论是打开的文件或任何其他的,返回的项目清单(或发电机)基于他们的进步项目数.所以,我建议使用更合适的通用名称;-).
Kin*_*Mak 27
为了提供另一种解决方案:
import linecache
linecache.getline('Sample.txt', Number_of_Line)
Run Code Online (Sandbox Code Playgroud)
我希望这很快捷:)
小智 14
如果你想要第7行
line = open("file.txt", "r").readlines()[7]
Mar*_*oma 10
阅读文件快速令人难以置信.读取100MB文件只需不到0.1秒(请参阅我的文章使用Python阅读和编写文件).因此,您应该完全阅读它,然后使用单行.
这里最常回答的不是错,而是糟糕的风格.应始终打开文件,with因为它确保文件再次关闭.
所以你应该这样做:
with open("path/to/file.txt") as f:
lines = f.readlines()
print(lines[26]) # or whatever you want to do with this line
print(lines[30]) # or whatever you want to do with this line
Run Code Online (Sandbox Code Playgroud)
如果你碰巧有一个巨大的文件和内存消耗是一个问题,你可以逐行处理它:
with open("path/to/file.txt") as f:
for i, line in enumerate(f):
pass # process line i
Run Code Online (Sandbox Code Playgroud)
为了完整起见,这里还有一个选择.
让我们从python docs的定义开始:
slice通常包含序列的一部分的对象.使用下标符号创建切片,[]在给定多个数字时使用数字之间的冒号,例如在variable_name [1:3:5]中.括号(下标)表示法在内部使用切片对象(或者在旧版本中使用__getslice __()和__setslice __()).
虽然切片表示法通常不直接适用于迭代器,但itertools包中包含替换函数:
from itertools import islice
# print the 100th line
with open('the_file') as lines:
for line in islice(lines, 99, 100):
print line
# print each third line until 100
with open('the_file') as lines:
for line in islice(lines, 0, 100, 3):
print line
Run Code Online (Sandbox Code Playgroud)
该函数的另一个优点是它直到最后才读取迭代器.所以你可以做更复杂的事情:
with open('the_file') as lines:
# print the first 100 lines
for line in islice(lines, 100):
print line
# then skip the next 5
for line in islice(lines, 5):
pass
# print the rest
for line in lines:
print line
Run Code Online (Sandbox Code Playgroud)
并回答原来的问题:
# how to read lines #26 and #30
In [365]: list(islice(xrange(1,100), 25, 30, 4))
Out[365]: [26, 30]
Run Code Online (Sandbox Code Playgroud)
其中一些很可爱,但是可以更简单地完成:
start = 0 # some starting index
end = 5000 # some ending index
filename = 'test.txt' # some file we want to use
with open(filename) as fh:
data = fin.readlines()[start:end]
print(data)
Run Code Online (Sandbox Code Playgroud)
这将仅使用列表切片,它会加载整个文件,但是大多数系统会适当地最小化内存使用,它比上面给出的大多数方法都快,并且可以处理我的10G +数据文件。祝好运!
如果您的大文本文件file结构严格(意味着每一行的长度相同l),您可以使用 for n-th line
with open(file) as f:
f.seek(n*l)
line = f.readline()
last_pos = f.tell()
Run Code Online (Sandbox Code Playgroud)
免责声明这仅适用于具有相同长度的文件!
小智 5
with open("test.txt", "r") as fp:
lines = fp.readlines()
print(lines[3])
Run Code Online (Sandbox Code Playgroud)
test.txt 是文件名,
打印 test.txt 中的第四行
| 归档时间: |
|
| 查看次数: |
485553 次 |
| 最近记录: |