I have multiple csv files.
in these files, "dot" used as a decimal marker.
I would like to change all dots to commas and then combine all csv files into one csv file. How can I do this in python?
I have multiple csv files.
in these files, "dot" used as a decimal marker.
I would like to change all dots to commas and then combine all csv files into one csv file. How can I do this in python?
As @Dazounet mentioned you can read file by file and line by line and replace "manually"
from os import listdir
from os.path import isfile, join
mypath = "pathToFolder/"
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
with open(mypath + "target.csv", 'a') as outfile:
for file in files:
with open(mypath+file, "r") as infile:
next(infile)
for line in infile:
line = line.replace("." , ",")
outfile.write(line)
The code is not tested
Using pandas
makes writing out the numbers with commas trivial:
df = pd.read_csv('path/to/file.csv')
)df.to_csv('path/to/file.csv', decimal=',')
)Note, this will add quotes to all your numbers, since the separator normally used is ,
. You can change that with the sep=';'
argument for instance.
I'll leave finding all the files to join as an exercise for the reader (see other answers here).
As for combining all the files into one file:
df = pd.concat([pd.read_csv(fname) for fname in files_to_convert])