[Solved] Modify csv files?


well firstly you need to find all the files in your directory that are CSV files:

import os
for file in os.listdir("/mydir"):
    if file.endswith(".txt"):
        #here is where we will process each file

now to process each file you need to open it:

csv_file = open(file, mode="r")

now we can read in the data:

data = file.readlines()

and close the file again:

csv_file.close()

assuming you want to edit them in place(no filename changes)
reopen the same file in write mode:

csv_file.open(file, mode="w")

now we process the data:

for line in data:
    file.write(line.replace(",",";"))

and now we close the file again:

csv_file.close()

and as this is all inside out ‘for file in …’ loop this will be repeated for each file in the directory that ends with ‘.csv’

2

solved Modify csv files?