python - Read multiple files with fileinput at a certain line -


I have many files that I need to open and read (I thought it was with fileinput.input () Can be easy). Those files are started on very relevant non-relevant information, this specific line id [tab] NAME [tab] geo [tab] feature (some time line 32, but unfortunately any other Line some time), then I want to store them in the list ("entries")

  id [tab] NAME [Tab] Geo [Tab] Feature 1 AM A1 2 BB B1 3 cc Way C1 .   

Now, before reading line 32 (see the code below), I would like to read from the above line. Is it possible to do this with fileinput? Or am I going wrong, is there another easy way to do this? So far my code is:

  entries = list for line in fileinput.input (): if fileinput.filelineno ()> 32: Entries .append (line.strip (). Split ("\ t"))   

I am trying to apply this idea with Python 3.2

UPDATE:

My code now looks, but still is out of range I need to add some entries to a dictionary Do I miss something?

  filelist = fileinput.input () entries = [] for the file name in the line fn: fn: if line.strip () == "id \ tname \ tGEO \ TFEATURE" : Break entries.extend (line.strip (.) In Fn. For the partition ("\ t")) for the entries in dic = collections.defaultdict (set): dic [e [1]]. E [3])   

Error:

  dic [e [1]]. (E [3]) Index error: List index out of range    

Just marker line Looking through the file again and after adding everything to the list.

Edit Your second problem occurs because not all rows in the original file are divided into at least 3 fields, for example a blank line is the result in an empty list , Therefore e [1] is invalid. I've updated the example with a nested iterator that filters the lines that are not the right size you want something different to the bar (probably empty rows but otherwise it says that divided the remaining rows on all 3 columns ), But you get this idea

  entries = [] filelist: fp in line with 'fn' as the line ('fn'): if Line.strip () == 'id \ tnam e \ tGEO \ tFEATURE: break # entries.extend (line.strip (). The line in the Split ( '(Teepiapi) ('. '  

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -