[Python] How to open csv files from different directories, do a calculation and open next...

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have two different directories with csv-files which are build like "xxx_1.csv" and "yyy_1.csv".

    I want to read that files and do a calculation with them.

    The calculation will be like

    1. iterate further through the directories
    2. read xxx_2.csv (from directory 1) and yyy_2.csv (from directory 2)
    3. Do the calculation to each row of xxx_2.csv with yyy_2.csv, example: additional operation. (r = x1 + y1) x1 == row 1 of xxx_2.csv and y1 == row 1 of yyy_2.csv
    4. Until there is no more next file to read in the directory.

    Example:

    dir1/
    xxx_1.csv (have 10 row)
    yyy_1.csv (have 4 row)
    ttt_1.txt
    zzz_1.csv (have 20 row)

    dir2/
    xxx_2.csv (must have 10 row)
    ttt_2.txt (must have 4 row)
    yyy_2.csv
    zzz_2.csv (must have 20 row)



    This assumes the number of files in dir 1 and dir 2 is equal, and the number of rows of csv on each index of file from dir 1 and dir 2 is equal.

    Continue reading...

Compartilhe esta Página