python - Split string ValueError: need more than 1 value to unpack -
i trying split string, , valueerror: need more 1 value unpack
i think understand, why error occurs, happens because there no value split right?
basically have loop returns strings xml
for channel in tree.findall("channel"): title = channel.find('title').text channelname,tvguide = title.split("[") print(channelname,tvguide)
when print out titles have this:
bein sports 1hd [07:00 - 07:30] + 106.8 min auto mundial
bein sports 2hd valueerror happens here?
bein sports 3hd [23:00 - 02:00] + 1216.8 min torino fc vs citta di palermo - italian league ( serie )
bein sports 4hd valueerror happens here?
bein sports 5hd [05:30 - 07:15] + 91.8 min olympique de marseille vs angers - french league 1 2015 - 2016 week 7
my question is, how fix loop split titles channelname & tvguide if strings dont contain tvguide?
for example in channels dont have tvguide (in example bein sports 2hd, bein sports 4hd) should make tvguide = " " or similar.
any ideas guys ?
instead of trying assigne channelname , tvguide separately, why not take work list split
method returns.
for channel in tree.findall("channel"): title = channel.find('title').text description = title.split("[") print description
this way need not worry if channel has name or tvguide, make sure getting string in channel object.
as jon clements suggested, still need figure out if allowed access description[1] , suggested elegant way str.partition
for channel in tree.findall("channel"): title = channel.find('title').text description = title.partition("[") # tuple 3 elements head, separator , tail. #head portion before separator, separator , tail rest of portion of string print description
Comments
Post a Comment