Search the web
Sign In
New User? Sign Up
PhillyPug · Python Users Group
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Reading tab-delimited numeric data into arrays in Python   Message List  
Reply | Forward Message #267 of 364 |
Re: Reading tab-delimited numeric data into arrays in Python


Thanks to Kevin and Andrew for your suggestions. I do know the exact
number of columns and rows of the files, but I forgot to mention that
the files contain headers for each column. I realized that this is
important, since I tried the abovementioned solutions and they both seem
to require only-numeric data. How can I tell python to ignore the
headers and import only the numeric data in the columns? Thanks so much
for your help. Best Regards, Peter




--- In PhillyPug@yahoogroups.com, Kevin Hill <khill@...> wrote:
>
> Peter Perez wrote:
> >
> >
> > Hi all,
> >
> > I need to read a tab-delimited file with numeric data in columns,
and
> > then create an array with each of the columns of the file. Although
I
> > am a newbie in Python, I thought it would be as easy (or easier)
than
> > in Matlab, but that doesn't seem to be the case. I haven't found any
> > way to do that, neither in the tutorials nor the online
documentation.
> > So far, I only know how to do this:
> >
> > filename = 'ptrace.csv'
> > reader = csv.reader(open(filename,'r'),dialect='excel-tab')
>
> You should use the "rb" flag - csv likes the file opened as binary.
>
> >
> > But I don't know how to load the numeric data into an array that
Python
> > can manipulate. Could somebody help me please? Thank you very much
for
> > your kind attention. Best regards, Peter
>
> Do you know how many columns you have in the file? And if so, does the
> column count vary for each row?
>
> Something like this would work:
>
> col1 = []
> col2 = []
> col3 = []
>
> def populateColumns(r):
> col1.append(r[0])
> col2.append(r[1])
> col3.append(r[2])
>
> reader = csv.reader(open(filename,'rb'),dialect='excel-tab')
> for row in reader:
> populateColumns(row)
>
> Just define as many lists as you need - one list per column.
>
> - khill
>






Fri Feb 22, 2008 7:36 pm

plpd00
Offline Offline
Send Email Send Email

Forward
Message #267 of 364 |
Expand Messages Author Sort by Date

Hi all, I need to read a tab-delimited file with numeric data in columns, and then create an array with each of the columns of the file. Although I am a newbie...
Peter Perez
plpd00
Offline Send Email
Feb 22, 2008
6:24 pm

... You should use the "rb" flag - csv likes the file opened as binary. ... Do you know how many columns you have in the file? And if so, does the column...
Kevin Hill
khill3210
Offline Send Email
Feb 22, 2008
6:36 pm

Thanks to Kevin and Andrew for your suggestions. I do know the exact number of columns and rows of the files, but I forgot to mention that the files contain...
Peter Perez
plpd00
Offline Send Email
Feb 22, 2008
7:36 pm

... You could just wrap the function call to int() or float() in a try/except block. That would catch the exception and handle it properly: col1 = [] col2 =...
Kevin Hill
khill3210
Offline Send Email
Feb 22, 2008
8:12 pm

This will all get read as strings, and you'll have to do the data conversion yourself. For each row (if it's all data), you should be able to something like: ...
Andrew Gwozdziewycz
jyrixx
Offline Send Email
Feb 22, 2008
6:49 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help