473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Nicer way of strip and replace?

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
Oct 11 '05 #1
6 2390
Markus Rosenstihl <mr*********@macnews.de> writes:
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.
Oct 11 '05 #2
> 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
Oct 11 '05 #3
On 2005-10-11 23:45:46 +0200, Paul Rubin <http://ph****@NOSPAM.invalid> said:
Markus Rosenstihl <mr*********@macnews.de> writes:
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.


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)


Oct 11 '05 #4
Markus Rosenstihl <mr*********@macnews.de> writes:
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.
Oct 11 '05 #5
Markus Rosenstihl wrote:
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
Oct 12 '05 #6
On 2005-10-12 01:29:28 +0200, Paul Rubin <http://ph****@NOSPAM.invalid> said:

Oh cool. I think you could even say:

rechnung = list(read_file)

instead of the for loop.


Yes, that is working too.
Oct 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self,...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
3
by: Michal A. Valasek | last post by:
Hello, I want to transform text with HTML markup to plain text. Is there some simple way how to do it? I can surely write my own function, which would simply strip everything with < and >....
5
by: dan.j.weber | last post by:
I'm using Python 2.3.5 and when I type the following in the interactive prompt I see that strip() is not working as advertised: >>>s = 'p p:p' >>>s.strip(' :') 'p p:p' Is this just me or...
2
by: ImageAnalyst | last post by:
Tom, Nikolay: That code doesn't work, at least not in VS2005. What happens is that when you replace with VBNullChar, it basically chops off the string from that point onwards. So Sna?*|fu" would...
6
by: eight02645999 | last post by:
hi can someone explain strip() for these : 'example' when i did this: 'abcd,words.words'
6
by: george | last post by:
hello, which is the best way to strip jscript/vbscript from user input? Is there any module I could reuse? thanks in advance george P.S. the solution must allow users to enter html code.
3
by: Colin J. Williams | last post by:
The Library Reference has strip( ) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed....
4
by: Poppy | last post by:
I'm using versions 2.5.2 and 2.5.1 of python and have encountered a potential bug. Not sure if I'm misunderstanding the usage of the strip function but here's my example. var = "detail.xml"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.