0

i have a csv file with data separated by " ; ". There is no problem reading the file but i want to export the data that was in the first csv no ANOTHER csv and add 1 new column to the new csv file.

import csv

with open('1.csv','r') as csvinput:
    with open('agg.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput)
        reader = csv.reader(csvinput,lineterminator=';')

        all = []
        row = next(reader)
        row.append('Movimiento')
        all.append(row)

        for row in reader:
            row.append(row[0])
            all.append(row)

        writer.writerows(all)

output csv why this happend?

4 Answers4

0

You would have to create a csv writer object, and use that object to write in it the required csv file. If you can tell me a little more about the column proble, I will be able to help better with that.

csvwriter = csv.writer("1.csv", delimiter=',')
Anant Mittal
  • 1,923
  • 9
  • 15
0

if you can use pandas library

try the following code.

import pandas as pd
data = pd.read_csv('<file_name>', sep =';')
#add new column
data['new_column_name']=[list_of_values]
data.to_csv('<to_filename>', index=False)
Vikas Jain
  • 74
  • 6
0

Try this:

with open('1.csv','r') as csvinput:
    with open('agg.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput, delimiter=',')
        reader = csv.reader(csvinput,lineterminator=';')

        all = []
        row = next(reader)
        row.append('Movimiento')
        all.append(row)

        for row in reader:
            row.append(row[0])
            all.append(row)

        writer.writerows(all)
Yash Gupta
  • 331
  • 2
  • 8
0

The data has ; as separator, so you probably want to specify that instead of lineterminator:

writer = csv.writer(csvoutput)
reader = csv.reader(csvinput, delimiter=';')
Danny_ds
  • 11,201
  • 1
  • 24
  • 46