我有一个包含数百万行数据的大文本文件。第一列包含位置坐标。我需要从这个原始数据创建另一个文件,但只包含基于位置坐标的指定的非连续区间。我有另一个文件包含每个间隔的坐标。例如,我的原始文件的格式与此类似:从python的大文本文件中有效地读取部分
Position Data1 Data2 Data3 Data4
55 a b c d
63 a b c d
68 a b c d
73 a b c d
75 a b c d
82 a b c d
86 a b c d
然后让说我有一个包含间隔我的文件看起来是这样的......
name1 50 72
name2 78 93
然后我想我的新文件看起来像这样...
Position Data1 Data2 Data3 Data4
55 a b c d
63 a b c d
68 a b c d
82 a b c d
86 a b c d
到目前为止,我已经创建了一个函数从包含在特定的时间间隔,以我的新文件中的原始文件写入数据。我的代码如下:
def get_block(beg,end):
output=open(output_table,'a')
with open(input_table,'r') as f:
for line in f:
line=line.strip("\r\n")
line=line.split("\t")
position=int(line[0])
if int(position)<=beg:
pass
elif int(position)>=end:
break
else:
for i in line:
output.write(("%s\t")%(i))
output.write("\n")
我然后创建一个使用类似这样的上述功能包含我的间隔通过我的原始文件对,然后循环列表:
#coords=[[start1,stop1],[start2,stop2],[start3,stop3]..etc]
for i in coords:
start_p=int(i[0]) ; stop_p=int(i[1])
get_block(start_p,stop_p)
这将执行我想要什么但是当它沿着我的坐标列表移动时,它会呈指数级变慢,因为我不得不通读整个文件,直到每次通过循环都达到指定的起始坐标。有没有更有效的方法来完成这个?有没有办法每次跳到一个特定的行,而不是阅读每一行?
你是什么意思?它运行,只是非常缓慢,这是我的代码。 – abovezero
感谢您收到错误信息,我必须添加一个额外的\错误。它现在已经修复。 – abovezero
'pandas'可能值得研究 – dm03514