Adding a column in python CSV and listing it

my CSV looks like

John,Bomb,Dawn
3,4,5
3,4,5
3,4,5

I want to add an identifier column in front so:

ID,John,Bomb,Dawn
1,3,4,5
2,3,4,5
3,3,4,5

using an enumeration function, but I don't know how to do this. Here is my code:

import csv

with open("testi.csv", 'rb') as input, open('temp.csv', 'wb') as output:
        reader = csv.reader(input, delimiter = ',')
        writer = csv.writer(output, delimiter = ',')

        all = []
        row = next(reader)
        row.append('ID')
        all.append(row)
        count = 0
        for row in reader:
                count += 1
                while count:
                        all.append(row)
                        row.append(enumerate(reader, 1))
                        break
        writer.writerows(all)

And the result does not come like this:

John,Bomb,Dawn,ID
3,4,5,<enumerate object at 0x7fb2a5728d70>
3,4,5,<enumerate object at 0x1764370>
3,4,5,<enumerate object at 0x17643c0>

So, the identifier comes at the end, when it should be at the beginning, and it does not even do 1,2,3. Some strange error comes up.

+4
source share
3 answers

I can suggest the code below to solve your question:

import csv

with open("testi.csv", 'rb') as input, open('temp.csv', 'wb') as output:
    reader = csv.reader(input, delimiter = ',')
    writer = csv.writer(output, delimiter = ',')

    all = []
    row = next(reader)
    row.insert(0, 'ID')
    all.append(row)
    for k, row in enumerate(reader):
        all.append([str(k+1)] + row)
    writer.writerows(all)

More compact code could be:

all = [['ID'] + next(reader)] + [[str(k+1)] + row for k, row in enumerate(reader)]

UPDATE (some explanation):

enumerate. enumerate for, enumerate, , , - .

enumerate (docs), , , __repr__ magic <enumerate object at ...>.

, enumerate , count += 1.

:

while count:
    all.append(row)
    row.append(enumerate(reader, 1))
    break

.

+3

insert() append(). , .

import csv

with open("testi.csv", 'rb') as input, open('temp.csv', 'wb') as output:
    reader = csv.reader(input, delimiter = ',')
    writer = csv.writer(output, delimiter = ',')

    all = []
    row = next(reader)
    row.insert(0, 'ID')
    all.append(row)
    count = 0
    for row in reader:
        count += 1
        row.insert(0, count)
        all.append(row)
    writer.writerows(all)
+3

- :

import csv

with open('testi.csv') as inp, open('temp.csv', 'w') as out:
    reader = csv.reader(inp)
    writer = csv.writer(out, delimiter=',')
    #No need to use `insert(), `append()` simply use `+` to concatenate two lists.
    writer.writerow(['ID'] + next(reader))
    #Iterate over enumerate object of reader and pass the starting index as 1.
    writer.writerows([i] + row for i, row in enumerate(reader, 1))

enumerate()returns an enumeration object that gives the index and element in the tuple one at a time, so you need to iterate over the object enumerateinstead of writing it to the csv file.

>>> lst = ['a', 'b', 'c']
>>> e = enumerate(lst)
>>> e
<enumerate object at 0x1d48f50>
>>> for ind, item in e:
...     print ind, item
...     
0 a
1 b
2 c

Conclusion:

>>> !cat temp.csv
ID,John,Bomb,Dawn
1,3,4,5
2,3,4,5
3,3,4,5
+2
source

Source: https://habr.com/ru/post/1537830/


All Articles