2

I am trying to add a column to a csv file that combines strings from two other columns. Whenever I try this I either get an output csv with only the new column or an output with all of the original data and not the new column.

This is what I have so far:

with open(filename) as csvin:
    readfile = csv.reader(csvin, delimiter=',')
    with open(output, 'w') as csvout:
        writefile = csv.writer(csvout, delimiter=',', lineterminator='\n')
        for row in readfile:
            result = [str(row[10]) + ' ' + str(row[11])]
            writefile.writerow(result)

Any help would be appreciated.

3
  • [str(row[10]) + ' ' + str(row[11])] would only be one column - you don't attempt to write anything else to the row and there is no comma delimiter. Are you trying to combine row[10] and row[11] into a single value that should be in its own column, in addition to the existing columns? Commented Sep 27, 2016 at 19:55
  • Yes I am trying to combine rows 10 and 11 into one column, and add that column onto the existing csv. Commented Sep 27, 2016 at 19:58
  • 1
    @glayne but you don't add it to row: writefile.writerow(row + result) Commented Sep 27, 2016 at 19:58

3 Answers 3

2

No input to test, but try this. Your current approach doesn't include the existing data for each row that already exists in your input data. extend will take the list that represents each row and then add another item to that list... equivalent to adding a column.

import csv
with open(filename) as csvin:
    readfile = csv.reader(csvin, delimiter=',')
    with open(output, 'w') as csvout:
        writefile = csv.writer(csvout, delimiter=',', lineterminator='\n')
        for row in readfile:
            row.extend([str(row[10]) + ' ' + str(row[11])])
            writefile.writerow(row)
Sign up to request clarification or add additional context in comments.

5 Comments

Is row a list or a tuple?
@PeterWood I had a mistake earlier, now corrected. But csv.reader should return a nested list, with each row a sub-list
It's a list: from the docs Each row read from the csv file is returned as a list of strings
You open output twice
@HaiVu well spotted. I started to convert the presented code to what I would normally do, then decided just to stick with initial. Thanks, I'll correct.
2

I assume that glayne wants to combine column 10 and 11 into one. In my approach, I concentrate on how to transform a single row first:

def transform_row(input_row):
    output_row = input_row[:]
    output_row[10:12] = [' '.join(output_row[10:12])]
    return output_row

Once tested to make sure that it works, I can move on to replace all rows:

with open('data.csv') as inf, open('out.csv', 'wb') as outf:
    reader = csv.reader(inf)
    writer = csv.writer(outf)
    writer.writerows(transform_row(row) for row in reader)

Note that I use the writerows() method to write multiple rows in one statement.

1 Comment

I like this approach more than the others. It is easier to read and understand. The transform_row() function can become more generic by removing the hardcoded list slicing and adding the required arguments, thus making it a reusable tool.
0

Below code snippet combines strings in column 10 and column 11 in each row and add that to the end of the each row

import csv
input = 'test.csv'
output= 'output.csv'
with open(input, 'rb') as csvin:
    readfile = csv.reader(csvin, delimiter=',')
    with open(output, 'wb') as csvout:
        writefile = csv.writer(csvout, delimiter=',', lineterminator='\n')
        for row in readfile:
            result = row + [row[10]+row[11]]
            writefile.writerow(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.