-1

I have multiple csv files.

enter image description here

in these files, "dot" used as a decimal marker.

enter image description here

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?

Qiu
  • 5,651
  • 10
  • 49
  • 56
Anahita Kp
  • 13
  • 1
  • 3
  • Possible duplicate of: https://stackoverflow.com/questions/6630170/python-parse-csv-file-replace-commas-with-colons & https://stackoverflow.com/questions/45256104/python-csv-change-separator – Wilmar van Ommeren Oct 11 '18 at 13:07
  • @Qiu That question changes the delimitter while op here asks to change decimal marker – Florian H Oct 11 '18 at 13:38

3 Answers3

0

You could parse csv row per row and then use replace() function on values where you want to replace .

Then, after this transformation, you write this row on a new CSV file.

Jan Černý
  • 1,268
  • 2
  • 17
  • 31
Dazounet
  • 1
  • 2
0

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

Florian H
  • 3,052
  • 2
  • 14
  • 25
  • thanks! this is the error that i got: files = [f for f in listdir(mypath) if isfile(join(mypath, f))] NameError: name 'listdir' is not defined – Anahita Kp Oct 11 '18 at 13:28
  • it destroyed the structure of the csv files. it added all the headers – Anahita Kp Oct 11 '18 at 13:52
  • just skip the first line with next(infile) to ignore the headers. i add it to the answer – Florian H Oct 11 '18 at 13:57
  • now it removes all headers and add more columns! is it possible to keep the first file with its header and append other fils without headers? – Anahita Kp Oct 11 '18 at 14:14
  • yes you could do that if you want. you might consider using pandas as Daren Thomas suggested. – Florian H Oct 11 '18 at 14:18
0

Using pandas makes writing out the numbers with commas trivial:

  • read in the csv file (df = pd.read_csv('path/to/file.csv'))
  • write out the csv file (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])
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200