newbie problem with str.replace

Discussion in 'Python' started by BobAalsma, Aug 4, 2010.

  1. BobAalsma

    BobAalsma Guest

    I'm working on a set of scripts and I can't get a replace to work in
    the script - please help.

    The scripts show no errors, work properly apart from the replace, all
    variables are filled as expected, the scripts works properly when the
    commands are copied to the Python shell.

    Text Main:
    ...
    from LeadDevice_klant_nieuw_bewerken_bestanden import omkattenBoom,
    omkattenNaam
    ....
    #
    # (3) omkatten bestandsnamen:
    #
    print 'Omkatten bestandsnamen'
    omkattenNaam(AANGRIJPINGSPUNT, KLANTNAAM_OUT, KLANTNAAM_IN,
    ZOEKSET1_OUT, ZOEKSET1_IN, ZOEKSET2_OUT, ZOEKSET2_IN)
    #
    .....


    Text LeadDevice_klant_nieuw_bewerken_bestanden:
    import os
    import distutils.core

    ....

    def omkattenNaam(AANGRIJPINGSPUNT, KLANTNAAM_OUT, KLANTNAAM_IN,
    ZOEKSET1_OUT, ZOEKSET1_IN, ZOEKSET2_OUT, ZOEKSET2_IN):
    #
    # Strings opbouwen voordat aan de lussen gewerkt wordt:
    #
    beginpunt = AANGRIJPINGSPUNT + KLANTNAAM_IN + '/'
    #
    ZOEKSET1_OUT_LOWER = ZOEKSET1_OUT.lower()
    ZOEKSET1_IN_LOWER = ZOEKSET1_IN.lower()
    ZOEKSET2_OUT_LOWER = ZOEKSET2_OUT.lower()
    ZOEKSET2_IN_LOWER = ZOEKSET2_IN.lower()
    #
    # Lussen:
    #
    for root, dirs, files in os.walk(beginpunt, topdown = False):
    for bestandsnaam in dirs and files:
    #
    bestandsnaam_nieuw = bestandsnaam
    bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)
    BobAalsma, Aug 4, 2010
    #1
    1. Advertising

  2. BobAalsma

    Peter Otten Guest

    BobAalsma wrote:

    Although [it] may not be obvious at first unless you're Dutch...

    > bestandsnaam_nieuw = bestandsnaam
    > bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)


    str.replace() does not modify a string, it creates a new one.

    This doesn't work:

    >>> s = "that's all folks"
    >>> s.replace("all", "nothing")

    "that's nothing folks"
    >>> s

    "that's all folks"

    But this does:

    >>> old = "that's all folks"
    >>> new = old.replace("all", "nothing")
    >>> new

    "that's nothing folks"

    Peter
    Peter Otten, Aug 4, 2010
    #2
    1. Advertising

  3. On Aug 4, 9:10 am, BobAalsma <> wrote:
    >                         #
    >                         bestandsnaam_nieuw = bestandsnaam
    >                         bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)


    The replace method does not modify the string (strings are immutable).

    You need to use the retun value of the method in an assignment, like
    so:

    bestandsnaam_nieuw = bestandsnaam.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

    This will not change the value of bestandsnaam
    Anthony Tolle, Aug 4, 2010
    #3
  4. BobAalsma

    BobAalsma Guest

    On Aug 4, 3:22 pm, Anthony Tolle <> wrote:
    > On Aug 4, 9:10 am, BobAalsma <> wrote:
    >
    > >                         #
    > >                         bestandsnaam_nieuw = bestandsnaam
    > >                         bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

    >
    > The replace method does not modify the string (strings are immutable).
    >
    > You need to use the retun value of the method in an assignment, like
    > so:
    >
    > bestandsnaam_nieuw = bestandsnaam.replace(KLANTNAAM_OUT,KLANTNAAM_IN)
    >
    > This will not change the value of bestandsnaam


    YESS!

    Thanks, this is what I wanted to achieve but could not find

    Regards,
    Bob
    BobAalsma, Aug 4, 2010
    #4
  5. BobAalsma

    Mike Kent Guest

    On Aug 4, 9:10 am, BobAalsma <> wrote:
    > I'm working on a set of scripts and I can't get a replace to work in
    > the script - please help.


    >                         bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)


    I'm not sure what you are intending to do here, but string.replace
    does not do its replacement in-place. It returns a copy of the
    original string, with the replacement done in the copy. You are not
    assigning the string returned by string.replace to anything,
    therefore, it is immediately thrown away.

    Secondly, and this is just a guess, but since you are doing the
    string.replace inside of an os.walk loop, you appear to be trying to
    do a filename change. I hope you realize that this will in no way
    change the name of the file *on disk*; it will only change it in
    memory.
    Mike Kent, Aug 4, 2010
    #5
    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. David
    Replies:
    2
    Views:
    477
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    629
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    337
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,702
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    305
    Chris
    Aug 11, 2008
Loading...

Share This Page