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
8 Respuestas
+ 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 '_'
+ 4
try using:
while ..... :
fnames=filename.split('_')
try:
f3=fnames[2]
except IndexError:
# print or something
+ 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?
+ 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....
+ 3
well...yup
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
0
you are suggesting to leave it as fnames=filename.split('_') and then retrieve fields as f3=fnames[2] or like f2 etc..
0
burey,
is there anyway for avoiding error and pass to next filename in the for loop like except