Search code examples
pythonloopscsvcsvreader

Pyhton csv reader: loop over csv files


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!


Solution

  • 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