Modify Csv Files?
Is it possible to modify all csv-files (240 in total) which are located in the same folder/directory with Python? The csv files consist test results and these are listed all in the
Solution 1:
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'
Solution 2:
simple solution with sed
sed -i 's/,/;/g' *.csv
Post a Comment for "Modify Csv Files?"