Question

I've data in a file like this:

196465702|91017022|['95592022', '96094942', '100920382']|91048702

and I will be transforming the data as below:

196465702|91017022|95592022|91048702
196465702|91017022|96094942|91048702
196465702|91017022|100920382|91048702

So far i've attempted to write this. Not sure if this is an accurate and optimal way. Moreover, If there is a list in 4th column and 5th column etc, it becomes tricky to loop for every column and join back.

import csv
with open('sample.out','r') as x:
  f = csv.reader(x,delimiter='|')
  for row in f:
    s = row[2].translate(None,"'")
    #print s.split(',')
    for t in s.split(','):
      print row[0]+'|'+row[1]+'|'+t.translate(None,"[None]")+'|'+row[3].translate(None,"None")

Is there any better way to write this? Additionally, if data has List in few columns, is there any better way to identify and transpose to rows and join back with rest of the data?

Was it helpful?

Solution

I would have done it like this

data = "196465702|91017022|['95592022', '96094942', '100920382']|91048702"
from ast import literal_eval
first, second, third, last = data.split("|")
third = literal_eval(third)
for item in third:
    print "{}|{}|{}|{}".format(first, second, item, last)

Output

196465702|91017022|95592022|91048702
196465702|91017022|96094942|91048702
196465702|91017022|100920382|91048702
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top