Python: loop through a file for specific lines -
i have following lines in file want take third column; in file don't have numbers column:
- red; blue; green; white; orange;
- green; white; orange;
- blue; green; white;
- red; blue; green; white;
- blue; green; white; orange;
- orange
- green; white; orange;
- white; orange
- green;
i used code line that:
lines = i.split(";")[2]
the problem of lines have 1 column or two, gives me 'index out of range' error. please tell me how go problem?
thanks lot adia
use slice instead of index.
>>> open('test.txt') f_in: ... column3 = (line.split(';')[2:3] line in f_in) ... column3 = [item[0] item in column3 if item] ... >>> column3 [' green', ' orange', ' white', ' green', ' white', ' orange']
Comments
Post a Comment