下面的程序,当文本文件中的记录数约为 6,000,000 PLUS 时,从由分隔符“~”分隔的文本文件中提取一些特定的列值会引发内存错误。但是,相同的片段适用于文本文件中较少数量的记录。我尝试使用列表理解,但无法解决内存错误。下面是代码片段:
import os
import csv
print("Analyzing the File!")
tnum = list()
nl = 0
total = list()
fdata = list()
amount = list()
with open(r"C:\Users\aojha\Desktop\20191125\ABC.TXT", "r") as fh:
next(fh)
for line in fh:
line = line.rstrip()
nl+=1
print("Number of records in file are:",nl)
with open(r"C:\Users\aojha\Desktop\20191125\ABC.TXT", "r") as fh:
next(fh)
fdata = [line.split("~") for line in fh]
##print(fdata)
for data in fdata:
tnum.append((data[2][:]).split(" "))
total.append((data[8][:]))
amount.append((data[13][:]))
#print(fdata)
print("Required data from file have been extracted!")}
样本输入数据格式如下:
P~LNL~22248370~50~22248370~20190916~20191112~20190916~002~I~A~N~003~1638~01~001~400023~-1552~20190916~0200058~001~X~~TMID~ ~~000~000~~000~000~000~~000~000~0~~~~N~~~
收到的错误如下:
> Traceback (most recent call last):
File "C:\Users\aojha\Desktop\Python\Random\Type_Count.py", line 23, in <module>
fdata = [line.split("~") for line in fh]
File "C:\Users\aojha\Desktop\Python\Random\Type_Count.py", line 23, in <listcomp>
fdata = [line.split("~") for line in fh]
MemoryError