Parsing numeric data from text file using Python -


I am trying to create a database from a numerical model output text file. There are four (4) rows of title block data in the text file, which are in accordance with the blocks of data in several rows (41,149), which are different from each 'internal' word and below it are some numerical data forms Line 1: Title Block Line 2: Title Block Line 3: Title Block Line 4: Title Block Line 5: Internal 1.0 (10 e 16.9) -1 Line 6: Data Data Data Data Line7: Data Data Data Data Line8 to Line8: Data Data Data Data Line26: Data Data Data Data Line27: Internal 1.0 (10 e 16.9) -1 Line 28: Data Data Data Data .. By all means 41,149 lines up to < P> Data blocks are not consistent size (i.e., there are more rows of data than some others). Thanks to a lot of help from this site, I am able to take 41,149 rows of data and organize each data block in different lists, from which I can parse and build a database. My problem is that this operation seems to last a long time. I was hoping someone could see the code given below and suggest me how I could be able to run it more efficiently. I can attach the model output file if necessary thanks!

  inFile = 'CONFINED_AQIFER.DIS' string = ['internal'] rowList = [] # create a list of each line number where a data block starts with the open (infile) such as MyFile: For Num, the line in Enumerate (myFile, 1): if any (in the line for the string for S): rowList.append (num) # line number received from DF Milllino (filename, Leno) Function to: if lineno & lt; 1: If there are no lines 1: line = f.readlines (100000) type line ("first line line 1"), then f = open (filename) line_read = 0 is not a return: returns none if line_dill + Lane (line) & gt; = Lineno: return line [lineno-lines_read-1] lines_read + = len (rows) # Arrange each data block in a unique list and add the last list (fList) to the line in the fList = [] row (line (line List [1:])): combined list = [] I = row list [row] data = [] while i  

< P> Some comments:

In Python 2, the xrange is always better than the range, the range creates the full list, while the exchange only gives an Iterator

See if you have these techniques Up to extrapolate more code.

Normally you do not want to allocate memory inside the loop (creating new lists)


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -