Nicer way of strip and replace?

Discussion in 'Python' started by Markus Rosenstihl, Oct 11, 2005.

  1. 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
     
    Markus Rosenstihl, Oct 11, 2005
    #1
    1. Advertising

  2. Markus Rosenstihl

    Paul Rubin Guest

    Markus Rosenstihl <> 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.
     
    Paul Rubin, Oct 11, 2005
    #2
    1. Advertising

  3. > 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
     
    Diez B. Roggisch, Oct 11, 2005
    #3
  4. On 2005-10-11 23:45:46 +0200, Paul Rubin <http://> said:

    > Markus Rosenstihl <> 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)
     
    Markus Rosenstihl, Oct 12, 2005
    #4
  5. Markus Rosenstihl

    Paul Rubin Guest

    Markus Rosenstihl <> 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.
     
    Paul Rubin, Oct 12, 2005
    #5
  6. Markus Rosenstihl

    Kent Johnson Guest

    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
     
    Kent Johnson, Oct 12, 2005
    #6
  7. On 2005-10-12 01:29:28 +0200, Paul Rubin <http://> said:
    >
    > Oh cool. I think you could even say:
    >
    > rechnung = list(read_file)
    >
    > instead of the for loop.


    Yes, that is working too.
     
    Markus Rosenstihl, Oct 12, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Arentz

    Is there a nicer way to do this?

    Stefan Arentz, Oct 4, 2007, in forum: Python
    Replies:
    12
    Views:
    462
    Victor B. Gonzalez
    Oct 5, 2007
  2. News123
    Replies:
    3
    Views:
    3,620
    Paul McGuire
    Jul 14, 2010
  3. Aquila
    Replies:
    35
    Views:
    492
    Mathieu Bouchard
    Mar 31, 2005
  4. S Kanakakorn
    Replies:
    3
    Views:
    398
    Jules
    Feb 9, 2007
  5. yelipolok
    Replies:
    4
    Views:
    279
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page