当前位置:主页 > 软件编程 > Python代码 >

用Python读取几十万行文本数据

时间:2021-08-31 09:59:13 | 栏目:Python代码 | 点击:

我在使用python读取几十万行的文件中的数据,并构造字典,列表等数据结构时,再访问字典,列表时,一般都会出现内存不够的问题,然后只能循环读取几百行或者一定数量的行数来循环操作。

keyword_list=[line.strip() for line in open("keywords.txt",'r')]
#f1=open("part_wiki_vec.txt",'r')
f1=open("wiki_vectors.txt")
f2=open("result.txt",'w')
i=0
content=f1.readlines()
while i<1150:
 for line in content[300*i:300*(i+1)]:
  line=line.strip().split(' ')
  if line[0] in keyword_list:
   wordvec=' '.join(line)
   print wordvec
   f2.write(wordvec)
  #print line
 i+=1

我是这样读取的

应该还有很多好的方法,比如多线程等等。

做此记录只为了学习

总结

您可能感兴趣的文章:

相关文章