Pyhton csv reader: loop over csv files
Question:
I have several csv files that are named: list_csv_X
with X going from 0 to 5. I would like to do a loop to read them with Python.
I am currently doing it manually:
for filepath in list_csv_0:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
for filepath in list_csv_1:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
for filepath in list_csv_2:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
How can I do it?
Thanks a lot!
Answers:
If I understand correctly, each of your 6 items is a list of filenames, yes?
I am assuming that, because you are doing this, and saying that it currently works:
for filepath in list_csv_0:
That would lead to an error, if list_csv_0
were a filename, because filepath
values would be individual characters of the filename, not ever a whole filename.
You can loop over the lists
list_csvs = [list_csv_0,list_csv_1,list_csv_2,list_csv_3,list_csv_4,list_csv_5]
for list_csv in list_csvs:
for filepath in list_csv:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
Use a simple for loop and an f-string as follows:
def process(filename):
with open(filename) as indata:
pass # process file here
for fileno in range(6):
with open(f'list_csv_{fileno}') as catalogue:
for line in catalogue:
process(line.strip())
I have several csv files that are named: list_csv_X
with X going from 0 to 5. I would like to do a loop to read them with Python.
I am currently doing it manually:
for filepath in list_csv_0:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
for filepath in list_csv_1:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
for filepath in list_csv_2:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
How can I do it?
Thanks a lot!
If I understand correctly, each of your 6 items is a list of filenames, yes?
I am assuming that, because you are doing this, and saying that it currently works:
for filepath in list_csv_0:
That would lead to an error, if list_csv_0
were a filename, because filepath
values would be individual characters of the filename, not ever a whole filename.
You can loop over the lists
list_csvs = [list_csv_0,list_csv_1,list_csv_2,list_csv_3,list_csv_4,list_csv_5]
for list_csv in list_csvs:
for filepath in list_csv:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
Use a simple for loop and an f-string as follows:
def process(filename):
with open(filename) as indata:
pass # process file here
for fileno in range(6):
with open(f'list_csv_{fileno}') as catalogue:
for line in catalogue:
process(line.strip())