Re: [Tutor] Tutor Digest, Vol 50, Issue 9

2008-04-09 Thread Kent Johnson
Kepala Pening wrote:
> import re
> 
> items = []
> for line in open('data.txt'):
> items.append(re.sub('\n', '', line).split(' '))

Hmm. So much to say about so little code!

- the re.sub() is not needed - the split() will remove the trailing newline:
In [53]: 'a b\n'.split()
Out[53]: ['a', 'b']

- you don't need re to replace a fixed character, you can use str.replace():
In [55]: 'a b\n'.replace('\n', '')
Out[55]: 'a b'

- If you just want to strip the trailing newline you can use strip() or 
rstrip(), with or without args, depending on how strict you want to be:
In [56]: 'a b\n'.strip()
Out[56]: 'a b'

- It's not clear that the OP wants a list of lines, but if so, a list 
comprehension is much more succinct:
items = [ line.split() for line in open('data.txt') ]
would do the job just fine.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 9

2008-04-09 Thread Kepala Pening

import re

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



- Original Message -
From: "Gloom Demon" <[EMAIL PROTECTED]>
To: tutor@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.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 9

2008-04-09 Thread linuxian iandsd
On Wed, Apr 9, 2008 at 12:59 PM, rui <[EMAIL PROTECTED]> wrote:

> Hi Gloom,
>
>
>
> You should give a look at the method "split" (of the string objects) and
> int.
>
> The first is used do break a string into smaller pieces and the other to
> convert a string to an int object, raising an exception when it is not
> possible.
>
>
> On Wed, Apr 9, 2008 at 9:29 AM, Gloom Demon <[EMAIL PROTECTED]> wrote:
>
> > 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.
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
>
> --
> Ruivaldo Neto
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

my guess would be something like this :

a=open('/home/some_file.txt')
for line in a:
  tmp_list_1=line.split()

  num_floats=0
  num_strings=0
   for i in tmp_list_1:
if type(i) == int:
 num_floats=num_floats+1
else:
 num_strings=num_strings+1

if you explain more you case maybe i can get more ideas - hope this helps
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 9

2008-04-09 Thread Kent Johnson
Gloom Demon wrote:
> Hello :-)
> 
> Can someone please explain to me ho can I find out how many elements are 
> there in one record of a list?

The len() function gives the length of a list.

> 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

You can also use slice indexing with negative numbers to index from the end:

In [50]: data = '''01 bla bla bla 23,15 2345,67
: 02 alb alb 2,4 890,1
: 03 bal bla alb lab 567,12345 87,45
: '''.splitlines()
In [51]: for line in data:
: line = line.split() # Break the line at whitespace
: print len(line) # Number of elements in the line
: print line[1:-2]
: print line[-2:]
: print
:
:
6
['bla', 'bla', 'bla']
['23,15', '2345,67']

5
['alb', 'alb']
['2,4', '890,1']

7
['bal', 'bla', 'alb', 'lab']
['567,12345', '87,45']

Negative indices index from the end of the list, so
   line[1:-2]
gives you the elements from line[1] up to but not including line[-2] 
which is the next-to-last element.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 9

2008-04-09 Thread rui
Hi Gloom,



You should give a look at the method "split" (of the string objects) and
int.

The first is used do break a string into smaller pieces and the other to
convert a string to an int object, raising an exception when it is not
possible.


On Wed, Apr 9, 2008 at 9:29 AM, Gloom Demon <[EMAIL PROTECTED]> wrote:

> 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.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Ruivaldo Neto
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 9

2008-04-09 Thread Gloom Demon
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.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor