0

I am trying to write a python script that writes data from one csv file into another csv file that is structured differently

I am currently only able to just duplicate the data in in the structure of the source file.

!/usr/bin/env python3
#imports csv module

import csv

#opens file and reads
out=open("forecast_test.csv","rb")
data=csv.reader(out)

with open('new_data.csv', 'w') as new_data:
        csv_writer = csv.writer(new_data)

        for line in data:
            csv_writer.writerow(line)
            print (line)

The structure of the source file is below

 id,
123,
435,
765,

I want my script to write it to a csv file that is structured like below

 id, id2, id3
123, 435, 765
2tone_tony
  • 47
  • 1
  • 10

1 Answers1

1

I hope it's okay that the first column is id0 not just id, but doing so saved the need of an if statement in a loop.

import csv

datafile=open('forecast_test.csv', 'r')
reader = csv.reader(datafile)

ls=[]
for r in reader:
    ls.append(r[0])

coltit=ls[0]

ls.pop(0)

lout=[]
lout1=[]
for x in ls:
    lout.append(str(coltit)+str(ls.index(x)))
    lout1.append(x)

with open('new_data.csv', 'w') as new_data:
    csv_writer = csv.writer(new_data)
    csv_writer.writerows([lout])
    csv_writer.writerows([lout1])

new_data.csv

id0,id1,id2
123,435,765
9716278
  • 2,044
  • 2
  • 14
  • 30
  • Hi Quick question though is there a way to keep the format below even if there isn't 5 indexes in the array? So if the source csv has 2 rows of id's but the target csv should keep the 3 other id columns, but the values would be blank. – 2tone_tony Jul 09 '19 at 19:03
  • @2tone_tony I want to answer your question, I just need some clarification. I'm sorry, I don't know what you mean by "format below even"? I think you want to edit an existing CSV with row[0] and [1] both having variables in them and now you want to tack on more rows to their side? I'd say take a look at , but do `list(map(list,reader))`, so that way you are just working with a 2D list. – 9716278 Jul 10 '19 at 06:49
  • @2tone_tony Also, if I understand your question right, for example if you start with `csv=[[id0,id1],[123,312],[435,543],[765,576]]` You should then be able to `csv[0] = csv[0] + [id2,id3,id4]` and `csv[1] = csv[1] + [454,554,654]`, and then just use write the CSV normally. Also take a look at , it might just be what you're looking for. – 9716278 Jul 10 '19 at 06:49
  • sorry I forgot to show an example after "format below" `source csv file id -- 123 234 564` expected result in target `id0 id1 id2 id3 id4 id5 -- -- -- -- -- -- 123 234 564` Imagine the ID's are headers and that the numbers are below them. – 2tone_tony Jul 10 '19 at 16:43
  • @2tone_tony `csv_writer.writerows([lout1])` to `csv_writer.writerows([['','','','','','']+lout1])`. If you want more help send me your code; email on profile. – 9716278 Jul 10 '19 at 21:31