-3

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 first column instead of several columns. With the modification I would like to replace the comma (,) with a semicolon (;) of all csv files to get example B) below:

A) Content example of one csv-file (before modification via Python script):

Column A:              Column B:   Column C:   Column D:   Column E:
1, 65, 165, 484, 15,
0, 46, 49, 4, 66,

etc.

B) Content example of one csv-file (after modification via Python script):

Column A:   Column B:   Column C:   Column D:   Column E:
1           65          165         484         15
0           46          49          4           66

etc.

I haven't found any example yet. Have you implemented something like that already? Or is there a better solution then Python? I know you can do it in Excel, but I don't want do repeat that 240 times.

Update: MY SOLUTION

I did it with the following code and it works. But between each row I have an empty row. I don't understand why??

with open('_' + csv_name, mode="w") as outfile:
    writer = csv.writer(outfile, delimiter=';')
    with open(csv_name, mode="rU") as infile:
        print(csv_name)
        reader = csv.reader(infile, dialect="excel")
        for row in reader:
            print(row)
            writer.writerow(row)

Every csv-file int the same folder as the *.py file is modified.

martineau
  • 119,623
  • 25
  • 170
  • 301
Kevin
  • 229
  • 2
  • 10
  • 19
  • 5
    Yes it's possible. Give it a shot and let us know where you get stuck. [Python CSV](https://docs.python.org/2/library/csv.html) – MrAlexBailey Mar 27 '15 at 13:49
  • Both examples `A)` and `B)` does not represent a csv file content. csv file means (A file which has comma separated values). In your case `Column A: Column B: ...` etc doesn't have any comma itself. And in B) if you are changing the second line of both example from ,(comma) to ; (semicolon), It is then not a csv file itself. – kvivek Mar 27 '15 at 14:01
  • there is more than one format of csv file, such as tab and semicolon delimited csv files – James Kent Mar 27 '15 at 14:28
  • Since there is no standard its pretty tough to argue wether something is or is not csv. I'd expect most importers to accept case `A)` as csv with a single field in the first row. – agentp Mar 27 '15 at 14:38
  • 1
    As @Jkdc points out, python has a nice csv library that is built in and you should really try using that. The 2 answers will solve your problem IFF your CSV data doesn't have any encodinged data (example... one row contains a column "A,B,C" where the commas inside are intended to be part of that column and not a delimter). Also note that your example is confusing; you say you want to replace the comma with a semicolon, but your post shows it being replaced with a tab) – Foon Mar 27 '15 at 15:12
  • Thanks for your answer! I'm very close what means it works...almost. Unfortunately I have a last question regaring the empty row (see above after the update). It would be great if you can give me a hint how I can avoid that!? – Kevin Mar 27 '15 at 16:32

2 Answers2

4

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'

James Kent
  • 5,763
  • 26
  • 50
  • Hi James. I wan't able to solve the problem written in the update above. That's why I use your version now and it works very vell. Thank you and have a nice week! – Kevin Mar 30 '15 at 06:19
  • i would imagine that what is ahppening with your code in youe update is that the csv reader is not stripping the trailing newline when it reads each line of data, but the writer is adding one each time, so you could try changing your last line to 'writer.writerow(row.rstrip())' – James Kent Mar 30 '15 at 10:59
1

simple solution with sed

sed -i 's/,/;/g' *.csv
josifoski
  • 1,696
  • 1
  • 14
  • 19