0

Would like to split fields of filenames inside a folder

getting error while splitting via first, second, third = filename.split('_') value error: too many values to unpack please note that I am splitting using for loop inside a folder

17th Feb 2017, 9:21 AM
chiju
chiju - avatar
8 Réponses
+ 5
filename.split('_') returns too many arguments for 'first,second,third' to handle just use one variable: fnames=filename.split('_') now fnames is a list of field names which were seperated by '_'
17th Feb 2017, 9:58 AM
Burey
Burey - avatar
+ 4
try using: while ..... : fnames=filename.split('_') try: f3=fnames[2] except IndexError: # print or something
17th Feb 2017, 10:48 PM
Burey
Burey - avatar
+ 3
then it is a problem since there is no guarantee about the amount of seperators and is there a problem with it being a list?
17th Feb 2017, 10:09 AM
Burey
Burey - avatar
+ 3
ahh i see the issue is exactly as you said on one file there might be correct amount of '_' but on another could be more or less you can try to get access to the 3rd field fnames=filename.split('_') f3=fnames[2] ..... problem here is if the amount of '_' is lower than expected....
17th Feb 2017, 10:18 AM
Burey
Burey - avatar
+ 3
well...yup
17th Feb 2017, 10:40 AM
Burey
Burey - avatar
0
thanks burey but I want to store the field in different variables my intention is to make the third field of file name as first field https://youtu.be/ve2pmm5JqmI I think the issue here is the different no of field of different filenames
17th Feb 2017, 10:12 AM
chiju
chiju - avatar
0
you are suggesting to leave it as fnames=filename.split('_') and then retrieve fields as f3=fnames[2] or like f2 etc..
17th Feb 2017, 10:37 AM
chiju
chiju - avatar
0
burey, is there anyway for avoiding error and pass to next filename in the for loop like except
17th Feb 2017, 8:28 PM
chiju
chiju - avatar