#1
  1. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,322
    Rep Power
    630

    Replacing " with ""


    I am creating a CSV file and some of the strings contain double quotes. For a CSV entry I need to escape the " with "" and can't seem to get it right. The string I'm trying to change is:

    This is a string with "double" quotes.
    Code:
    newstr=mystr.replace('"','""')
    I need newstr to contain:

    This is a string with ""double"" quotes.

    What I get is an unchanged string:

    This is a string with "double" quotes.

    TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    471
    Rep Power
    70
    It works all right, as the following IDLE session shows. Are you really using newstr after that replacement (not mystr)? Or would you need to write the strings to the file again?

    Code:
    >>> mystr = 'This is a string with "double" quotes.'
    >>> newstr = mystr.replace('"', '""')
    >>> newstr
    'This is a string with ""double"" quotes.'

    Comments on this post

    • b49P23TIvg agrees : supreme diagnosis
    My armada: Debian GNU/Linux 8 (desktop, home laptop, work laptop), Raspbian GNU/Linux 8 (nameserver), Ubuntu 14.04.3 LTS (HTPC), PC-BSD 10.2 (testbed), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,322
    Rep Power
    630
    Thanks for the reply. Dopey me. Sorry.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo