Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:56:54+00:00 2026-06-09T00:56:54+00:00

Have a function fix(), as a helper function to an output function which writes

  • 0

Have a function fix(), as a helper function to an output function which writes strings to a text file.

def fix(line):
    """
    returns the corrected line, with all apostrophes prefixed by an escape character

    >>> fix('DOUG\'S')
    'DOUG\\\'S'

    """
    if '\'' in line:
        return line.replace('\'', '\\\'')
    return line

Turning on doctests, I get the following error:

Failed example:
    fix('DOUG'S')
Exception raised:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest convert.fix[0]>", line 1
        fix('DOUG'S')
                  ^

No matter what combination of \ and ‘s I use, the doctest doesn’t seem to to want to work, even though the function itself works perfectly. Have a suspicion that it is a result of the doctest being in a block comment, but any tips to resolve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:56:56+00:00Added an answer on June 9, 2026 at 12:56 am

    Is this what you want?:

    def fix(line):
        r"""
        returns the corrected line, with all apostrophes prefixed by an escape character
    
        >>> fix("DOUG\'S")
        "DOUG\\'S"
        >>> fix("DOUG'S") == r"DOUG\'S"
        True
        >>> fix("DOUG'S")
        "DOUG\\'S"
    
        """
        return line.replace("'", r"\'")
    
    import doctest
    doctest.testmod()
    

    raw strings are your friend…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function some_func_1 which will create an object of type some_type and will
Inside my Controller i have function that runs after user clicks on item, which
I am using Symfony 1.3.2 on Ubuntu. I have written a little helper function
I have this function which is supposed to open up a child window using
I have a function which adds a control to a parent control which is
I have a function called block which simply return false; this is meant to
I have a function to display a notification, which I call from various activities.
i have a function that i can call to apply a rounded corner fix
I have a function (assign) in a class (graph) in a header file. The
I have a helper method that serialises an object, which works until you try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.