[Tutor] .txt matrix import

Prasad, Ramit ramit.prasad at jpmorgan.com.dmarc.invalid
Wed Aug 7 23:46:59 CEST 2013


Joshua Kim wrote:
> Hello,
> 
> 
> This is my first time using Tutor, so I apologize for any fallacies I may be making; my problem
> involves opening a .txt file which contains several lines of descriptive information followed by a tab
> delimited matrix of numerical values... I am trying to find an effective way in which to skip the first
> 'X' number of lines in the .txt file (54 in my case) and input the columns of data into individual
> arrays (note the 55th line denotes the title of the column, so preferably I would like to have python
> use those values as the names to the individual array, but I am unsure of how involved that may
> become)
> 
> Any hints/suggestions/pointers would be appreciated.
> 
> 
> Thanks


The csv module should be able to handle most common delimiters and also has a DictReader
which lets you load each line on dictionary. You can use next() on the filehandle
to skip lines. 

====== File data ======
blah
blah
blah
col1,col2,col3
d1,d2,d3
d4,d5,d6
=======================

>>> with open( filename, 'r' ) as f: # Might need 'rb' instead of 'r'
...     for _ in xrange(3): # skip arbitrary amounts.
...         _ = next(f) # use _ to avoid it showing on interpreter output
...     column_names = next(f).split(',') # split on your delimiting character
...     # might need to strip() on each column name
...     reader = csv.DictReader( f, fieldnames=column_names, )
...     for d in reader:
...         pprint.pprint(d)
...     
{'col1': 'd1', 'col2': 'd2', 'col3\n': 'd3'}
{'col1': 'd4', 'col2': 'd5', 'col3\n': 'd6'}


~Ramit



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list