简体   繁体   中英

Memory Error while running python script on 4GB file

I am trying to count number of words that has length between 1 and 5, file size is around 4GB end I am getting memory error.

import os 
files = os.listdir('C:/Users/rram/Desktop/') 
for file_name in files:     
    file_path = "C:/Users/rram/Desktop/"+file_name     
    f = open (file_path, 'r')    
    text = f.readlines()
    update_text = '' 
    wordcount = {}
    for line in text:         
        arr = line.split("|")
        word = arr[13]
        if 1<=len(word)<6:
            if word not in wordcount:
                wordcount[word] = 1
        else:
            wordcount[word] += 1
            update_text+= '|'.join(arr)
print (wordcount)     #print update_text
print 'closing', file_path, '\t', 'total files' , '\n\n'
f.close()

At the end i get a MemoryError on this line text = f.readlines()

Can you pelase help to optimize it.

As suggested in the comments you should read the file line by line and not the entire file.

For example :

count = 0
with open('words.txt','r') as f:
    for line in f:
        for word in line.split():
          if(1 <= len(word) <=5):
              count=count+1
print(count)

EDIT :

If you only want to count the words in 14-th colomun and split by "|" instead then :

count = 0
with open('words.txt','r') as f:
    for line in f:
        iterator = 0
        for word in line.split("|"):
            if(1 <= len(word) <=5 and iterator == 13):
                count=count+1
            iterator = iterator +1
print(count)

note that you should avoid to write this

arr = line.split("|")
word = arr[13]

since the line may contains less than 14 words, which can result in a segmentation error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM