[Tutor] Import tabular data file and assign to variables

Chad Crabtree flaxeater at yahoo.com
Thu Sep 16 19:59:42 CEST 2004


RenderPipe wrote:

>Hello,
>
>I'm having difficulty trying to figure out how to read in tabular
data
>to variables.
>
>I have a file with about 700 lines that look like this:
>    -31.5227      7.7864    -74.4018     -2.4335     -8.7991
>    -31.5227      7.7864    -74.4018     -2.4335     -8.7991
>
>These are coordinates for a 3d object in 3 translation axis and 2
>rotations axis. Each line is 1 frame in time.
>I need to be able to assign each field value to a variable but I
don't
>understand how to parse the list.
>
>Essentially what I need is 
>Frame 1
>transx = -31.5227
>transy = 7.7864
>transz = -74.4018
>rotx = -2.4335
>roty = -8.7991
>
>Frame 2
>etc
>
>I was thinking of creating a dictionary object for each frame but I
do
>not know how to extract the data in the colums and assing them to a
>dictionary.
>Here's what i have come up with so far:
>
>import string
>
>myfile = open("MyFile")
>data = myfile.readlines()
>for line in data:
>        rs  = line.split("\t")
>        print rs
>
>The problem with this is it also prints \r\n characters. The example
>output of this code is:
>['    -31.5227      7.7864    -74.4018     -2.4335     -8.7991\r\n']
>['    -31.5227      7.7864    -74.4018     -2.4335     -8.7991\r\n']
>['\r\n']
>
>I appreciate any input you can offer.
>  
>
here try this instead

for line in data:
        rs  = line.strip().split()
        print rs

this should give you what you want.  For additional information
.split() 
automaticly splits on whitespace, anywhite space, the reason this may

not be working right is because what you think is \t is actually 
spaces.  Good luck


		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 


More information about the Tutor mailing list