[Tutor] Tutor Digest, Vol 50, Issue 9

Kepala Pening kepalapening at gmail.com
Wed Apr 9 15:53:18 CEST 2008

import re

items = []
for line in open('data.txt'):
    items.append(re.sub('\n', '', line).split(' '))

----- Original Message -----
From: "Gloom Demon" <gloomdemon at gmail.com>
To: tutor at python.org
Date: Wed, 9 Apr 2008 15:29:35 +0300
Subject: Re: [Tutor] Tutor Digest, Vol 50, Issue 9

Hello :-)

Can someone please explain to me ho can I find out how many elements are 
there in one record of a list? 

The problem is as follows:

I have a txt file from which I read data into Python.

The file looks something like this:

01 bla bla bla 23,15 2345,67
02 alb alb 2,4 890,1
03 bal bla alb lab 567,12345 87,45

I need to be able to discriminate the string parts from the numeric ones. 
Since the number of words in the file can vary, I have to be able to find out 
when they are finished 
and when the floats come in

mystring[0]-> always integer
mystring[1]-> string (word)
mystring[1-X]-> last string (word)
mystring[X+1]-> always float
mystring[X+2]-> always float

it would have been nice if I could find out the total number of the fields in 
one list record so that I could then adress them via a variable.

More information about the Tutor mailing list