import csv,sys
filename = 'a.csv'
with open(filename,'rb') as csvfile:
reader = csv.reader(csvfile,delimiter=',')
try:
for row in reader:
if row[1].find(',') == -1:
line = ','.join(row)
print line
else:
for i in range(0,row[1].count(',')+1):
line = row[0]+','+row[1].split(',')[i]+','+row[2].split(',')[i]
print line
except csv.Error as e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
I don't think your file is a real csv file.
But you can try this codes ,I think it works.
def convert(strs):
array=[]
if len(strs)==4:
array.append({
'left':strs[0],
'right':strs[2]
})
array.append({
'left':strs[1],
'right':strs[3]
})
else:
array.append({
'left':strs[0],
'right':strs[1]
})
return array
file='/Users/lidl/example.csv'
sumarray=[]
for line in open(file):
line=",".join(line.split())# rebuild a str line with comma segmentation
strs=line.split(",")
sumarray=sumarray+convert(strs)
for item in sumarray:
print(item['left']+" "+item['right'])
Python Power Unleased :
Here is a quick Ruby version:
Test:
I don't think your file is a real csv file. But you can try this codes ,I think it works.
sorry for the codes bad format.