I have a large CSV with comma separated lines of varying length. Sorting another set of data I used split(',')
in a loop to separate fields, but this method requires each line to have the same number of entries. Is there a way I can look at a line and, independent of the total number of entries, just pull the Nth item? For reference, the method I was using will only work with a line that looks like AAA,BBB,CCC,DDD
entry = 'A,B,C,D' (a,b,c,d) = entry.split(',') print a,b,c,d
But I would like to pull A and C even if it looks like A,B,C,D,E,F or A,B,C.
Advertisement
Answer
Use a list instead of separate variables.
values = entry.split(',') print values[0], values[2]