Nicer way of strip and replace?

M

Markus Rosenstihl

Hi,
I wonder if i can make this nicer:

In my case a list is created from a bill which is a tab seperated file
and each line looks like this:

"Verbindungen Deutsche Telekom vom 03.08.2005 bis
10.08.2005" "ISDN" "Deutsche Telekom
AG" "Rufnummer" "123456789" "3" "Verbindungen zu T-Mobile
AktivPlus xxl sunday" "19483" "19" "0,1719" "3,27" "16"


I create the list in the following way:

rechnung_raw=open('filename', 'r').readlines()

for line in rechnung_raw:
the_line=string.split(line, '\t')
rechnung.append(the_line)

Now, I have a loop which does basically:

for element in anotherlist: #anotherlist contains line numbers
euro=euro + float(string.replace(string.strip(rechnung[element][10],
'"'), ',' , '.'))



I replace commata with dots, then delete leading/trailing "s then add
to a sum after converting it to a float.
This looks ugly (I htink) and I wonder if there is a nicer way to
strip commata and change the comma to a dot already when reading in.
Or should i do it with a shell script before processing in python?

Regards
Markus
 
P

Paul Rubin

Markus Rosenstihl said:
This looks ugly (I htink) and I wonder if there is a nicer way to
strip commata and change the comma to a dot already when reading in.
Or should i do it with a shell script before processing in python?

First of all you should just set your locale to use comma instead
of dot as the numeric decimal, instead of translating commas to dots.

Second, for the field separation, see if you can configure the csv
module to do it for you.
 
D

Diez B. Roggisch

I replace commata with dots, then delete leading/trailing "s then add to
a sum after converting it to a float.
This looks ugly (I htink) and I wonder if there is a nicer way to strip
commata and change the comma to a dot already when reading in.

It's not ugly - it's necessary.
Or should i do it with a shell script before processing in python?

Which would make it less "ugly"?

Diez
 
M

Markus Rosenstihl

First of all you should just set your locale to use comma instead
of dot as the numeric decimal, instead of translating commas to dots.

Yes that is true, but most of the time I use a dot as numerical decimal.

Second, for the field separation, see if you can configure the csv
module to do it for you.

indeed, it did it correctly by default:

read_file = csv.reader(open('2005_08_Rechnung_4963184011.dat', 'r'),
delimiter="\t")
for row in read_file:
rechnung.append(row)
 
P

Paul Rubin

Markus Rosenstihl said:
indeed, it did it correctly by default:

read_file = csv.reader(open('2005_08_Rechnung_4963184011.dat', 'r'),
delimiter="\t")
for row in read_file:
rechnung.append(row)

Oh cool. I think you could even say:

rechnung = list(read_file)

instead of the for loop.
 
K

Kent Johnson

Markus said:
Hi,
I wonder if i can make this nicer:

euro=euro + float(string.replace(string.strip(rechnung[element][10],
'"'), ',' , '.'))

You can use the str methods instead of functions from the string module:
euro=euro + float(rechnung[element][10].strip('"').replace(',' , '.'))

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top