0

Below program, which extracts some specific column values from a text file delimited by separator "~" is throwing memory error when the number of records in the text file are around 6,000,000 PLUS. However the same snippet is working for smaller number of records in the text file. I tried using list comprehension but not able to resolve the memory error. Below is the code snippet:

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!")}

Sample Input Data is in below format:

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~~~

Error received is as follows:

> 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

Amit O.
  • 9
  • 2
  • Tried using buffering as well as combination of buffering and reading line by line but still getting memory error. See below snippet: `with open(r"C:\Users\aojha\Desktop\Type 60\ABC.TXT", "r", buffering=100000) as fh: next(fh) ## fdata = [line.split("~") for line in fh] for line in fh: line = line.rstrip() if line: line = line.split("~") fdata.append(line)` – Amit O. Feb 04 '20 at 01:23
  • What is the purpose of the code - what problem is it intended to solve? Please edit the question to explain this. – kaya3 Feb 04 '20 at 01:29
  • @kaya3: Mentioned the purpose of the code. – Amit O. Feb 04 '20 at 01:45

1 Answers1

0

Finally, I have solved this problem. Used sqlite for storing the data as the text file in my case was of 5Gb in size and in order to store the csv/text data in sqlite, I used csv reader and read those data using generator function/

Amit O.
  • 9
  • 2