网络编程
位置:首页>> 网络编程>> Python编程>> 用Python读取几十万行文本数据

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

作者:accumulate_zhang  发布时间:2023-10-13 21:56:04 

标签: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

我是这样读取的

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

做此记录只为了学习

来源:https://blog.csdn.net/accumulate_zhang/article/details/52705382

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com