python - How can I cut a variable in a for loop each time differently? -
i have document called 'asin.txt':
in,huawei1,de.fr.it.uk out,huawei2,de.fr.it out,huawei3,fr in,huawei4,fr.it in,huawei5,it
i'm opening file , make ordereddict:
from collections import ordereddict reader = csv.reader(open('asin.txt','r'),delimiter=',') reader1 = csv.reader(open('asin.txt','r'),delimiter=',') d = ordereddict((row[0], row[1].strip()) row in reader) d1 = ordereddict((row[1], row[2].strip()) row in reader1)
than create 'for' loop:
from itertools import izip (a, b), (c, e) in izip(d.items(), d1.items()): """in first time: a='in'; b='huawei1'; c='huawei1'; e='de.fr.it.uk'""" c1, c2, c3, c4 = c.split('.') # i'm cutting variable 'c' try: ...........
but python give me error:
c1, c2, c3, c4 = c.split('.') valueerror: need more 3 values unpack
i know why have error, because in first line of 'asin.txt':
in,huawei1,de.fr.it.uk
after second coma have 4 countries , , in second line of 'asin.txt'
out,huawei2,de.fr.it
after second coma have 3 countries. , have statement c1, c2, c3, c4 = c.split('.')
.
how can make program cuts c each time on pieces countries in asin.txt?
so exemple in first lap cuts on 4 pieces on second on 3 pieces on third on 1 piece....
you can store results of split
list:
clist = c.split('.')
this way, c
"cut" same number of pieces countries in asin.txt.
Comments
Post a Comment