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.
row.appenddoes not return anything. What isnsupposed to be? Ifi = [], what iswhile i < 167supposed to mean?i) with a number (167). So thatwhileloop either never runs → no defining ofn, or will be an endless loop.