문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top