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:

  1. red; blue; green; white; orange;
  2. green; white; orange;
  3. blue; green; white;
  4. red; blue; green; white;
  5. blue; green; white; orange;
  6. orange
  7. green; white; orange;
  8. white; orange
  9. 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

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -