Ограничение памяти достигнуто, потому что вы пытаетесь загрузить весь CSV в память.Простым решением было бы читать файлы построчно (при условии, что все ваши файлы имеют одинаковую структуру), управлять им, а затем записывать его в целевой файл:
filenames = ["file1.csv", "file2.csv", "file3.csv"]
sep = ";"
def check_data(data):
# ... your tests
return True # << True if data should be written into target file, else False
with open("/path/to/dir/result.csv", "a+") as targetfile:
for filename in filenames :
with open("/path/to/dir/"+filename, "r") as f:
next(f) # << only if the first line contains headers
for line in f:
data = line.split(sep)
if check_data(data):
targetfile.write(line)
Обновление : Пример метода check_data
, следующий за вашими комментариями:
def check_data(data):
return data[n] == 'USA' # < where n is the column holding the country