covfefe
covfefe

Reputation: 1

Merge two rows in a csv file in Python

I am very new to Python and I am trying to do a very simple merge of every two lines in a csv file. Basically I want it like this:

Input:

[H1],1,2,3,4
[H2],5,6,7,8
[H1],a,b,c,d
[H2],e,f,g,h

Output:

[H1],1,2,3,4,[H2],5,6,7,8
[H1],a,b,c,d,[H2],e,f,g,h

This is a brief example, but the csv file has up to 167 columns with the two lines combined. This is what I have:

import csv
f = open("sample.csv",'rU').read().split("\n")
reader = csv.reader(f)
for row in reader:
    if row[0].startswith("[H1]"): 
        i=[]
        while i<167: n = row.append([",".join(row[i]+row[i+1])])
        print n

However when I run it I get the following error:

    print n
NameError: name 'n' is not defined

Any help is appreciated, thanks.

Upvotes: 0

Views: 8331

Answers (4)

nvd
nvd

Reputation: 3381

import csv

f = open("sample.csv",'rU').read().split("\n")
reader = csv.reader(f)

i = 0

for row in reader:
    if i % 2 == 0:
        line = row
    else:
        line = line + row
        print ", ".join(line)

    i += 1

Upvotes: 1

user1907906
user1907906

Reputation:

Input i.csv:

1,2,3
foo,bar,baz
4,5,6
qux,quux.quuux

Python codce:

with open("i.csv") as f:
    reader = csv.reader(f)
    i = 0
    for row in reader:
        if i % 2 == 0:
            newRow = row
        else:
            newRow = newRow + row
            print(newRow)
        i = i + 1

Output:

['1', '2', '3', 'foo', 'bar', 'baz']
['4', '5', '6', 'qux', 'quux', 'quuux']

Upvotes: 1

BlackJack
BlackJack

Reputation: 4689

Here is a way to combine the line pairs:

import csv
from itertools import izip


def main():
    with open('sample.csv', 'rb') as input_file:
        reader = csv.reader(input_file)
        for even_row, odd_row in izip(reader, reader):
            combined_row = even_row + odd_row
            print combined_row


if __name__ == '__main__':
    main()

Upvotes: 0

Writing while i<167: n = row.append([",".join(row[i]+row[i+1])]) is like writing:

while i<167:
    n = row.append([",".join(row[i]+row[i+1])])

So the scope of n is the loop block. Your print n is out of that scope thus raising NameError.

You could add n = None right before the while:

n = None
while i<167: n = row.append([",".join(row[i]+row[i+1])])
print n

Or move print n into the loop block:

while i<167:
    n = row.append([",".join(row[i]+row[i+1])])
    print n

Note that any of these changes will avoid your program interruption by the out of scope error but you will print a lot of lines containing None because append returns None: https://docs.python.org/2/tutorial/datastructures.html

Upvotes: 0

Related Questions