It says
james.append(sanitize(each_item))
(mins, secs) = time_string.split(splitter)
need more than 1 value to unpack
What's wrong? Thank you. It is the example in Head First Python
james = []
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = '-'
else:
splitter = ','
(mins, secs) = time_string.split(splitter)
return(mins + '.' + secs)
with open('james.txt', 'r') as jaf:
data = jaf.readline()
james_data = data.strip().split(',')
for each_item in james_data:
james.append(sanitize(each_item))
sort(james)
print(james)
I'm pretty sure the second if should have
elif ':' in time_string:
splitter = ':'
Basically it looks for strings of type: min:sec or min-sec or min,sec and tries to convert them into min.sec
However currently on anything other then min-sec it will fail because split will not have anything valid to work on.
So it looks like it will work only on a csv file with pairs like:
min-sec, min-sec, min-sec,
in the first line
If you fix the second 'if' it will also work on min:sec.. but it should also handle other tokens that don't have either : or - maybe just return them as is?
It will also fail on values such as <???>-<????>-<???>
with too many values to unpack
Sanitize is looking for a string to split its input once it decides on a splitter it tries to split the string using it then rebuild it..
However if the splitter appears more then once it will fail since there will be too may values to unpack. And the default splitter is ,
which is the only character that can't possibly appear in this text ever
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.