0

I have csv file. It does not have column names. I need to append new column at the start of the row. I mean array[0]. I am new to this. Please help me.

This is the csv file I have

1,100,303,619,figure
338,162,143,423,text
85,768,554,39,text
504,164,24,238,subtitle

I just want to add new column Record like this

Record_01,98,87,544,396,figure
Record_02,98,488,91,48,caption
Record_03,98,568,142,254,figure
Record_04,97,834,8,6,page

I have tried this code it doesn't work. it doesn't show error message. But it doesn't change anything.

key = []
for row, Record in enumerate(key):
    print('Record_{}'.format(row, Record))
    row.append(row[0])
    key.appen(row)
            with open('csvfiles', 'r') as csvinput:
                with open('csvfiles', 'w') as csvoutput:
                    writer = csv.writer(csvoutput, delimiter=',')
                    writer.writerow[key] 

Highly appreciate for any answers.

Wai
  • 63
  • 6
  • I didn't got any answer and I am asking only one problem this time. If you can provide code greatly aprreciate – Wai Jul 16 '20 at 10:06
  • ***If you can provide code***: You don't want a answer, you want someone write a working code for you. – stovfl Jul 16 '20 at 10:11

2 Answers2

2

based on your own answer that you deleted, you were fairly close. The change is that instead of writer.writerows(data) you can iterate over the lines and change each line to have an extra entry.

import csv
#load the file

with open('00001.csv') as input_file:
#read the input csv file 
    reader = csv.reader(input_file)
    data = [line for line in reader]
#load the the output file
with open('00001.csv','w') as output_file:
    writer = csv.writer(output_csvfile)
    #add the column name
    writer.writerow(["X", "Y", "Width", "Height", "Tag"])
    # for each line including indices
    for i,line in enumerate(data, start=1):
        # insert extra entry at beginning of line (index 0)
        line.insert(0,"Record_{}".format(i))
        # write the row.
        writer.writerow(line)
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
0

For those of you who might encounter this type of problem I fount the answer. There is no way to add header alone. You have to re-write whole csv file. This code might help you.

import csv
#load the file

with open('00001.csv', newline='') as input_file:
#read the input csv file 
    reader = csv.reader(input_file)
    data = [line for line in reader]
#load the the output file
with open('00001.csv','w',newline='') as output_file:
    writer = csv.writer(output_csvfile)
    #add the column name
    writer.writerow(["X", "Y", "Width", "Height", "Tag"])
    writer.writerows(data)
Wai
  • 63
  • 6
  • 1
    you are lucky I had enough rep to see this while it was deleted, if you had posted this code and a question like "how would I modify this to add a column?" might have had better reception, at least more people commenting "writerows writes all data, you need to modify data" – Tadhg McDonald-Jensen Jul 17 '20 at 16:25
  • @TadhgMcDonald-Jensen You are right. I didn't have any idea. Its my bad. I should have choose the right keywords. Thank you so much for pointing out. I will be more careful next time. – Wai Jul 17 '20 at 16:29
  • 1
    No worries! I totally get how challenging it can be to get started, I firmly believe that seeing examples of small changes to code you already understand is very helpful to learn how to manipulate examples you may see in other questions or documentation. And without already having that kind of experience **it is super hard to ask for help.** As it stands your question is pretty good, you have sample input, expected output, intent is very clear, attempt is there, actual behaviour is given. You have all the right elements! – Tadhg McDonald-Jensen Jul 17 '20 at 16:47