Inconsistent Whitespace error occurrence when using Python doctest with newline characters

0 votes

Hi all. My question is simple. When you guys write Python doctests, how do you go about introducing the newline characters in the string while testing the process?

Check out the following example:

def remove_newlines(text):
    """
    >>> remove_newlines("line1 \n"
    ...                 "still line 1\r"
    ...                 "now line2 \n"
    ...                 "more line2\n")
    line1 still line1
    now line2 more line2
    """
    return text.replace('\n', '')

import doctest
doctest.run_docstring_examples(remove_newlines, globals())

The output is something like this:

Traceback (most recent call last):
...
ValueError: line 3 of the docstring for NoName has inconsistent leading whitespace: '"'

What am I missing here lads? Using this in a project of mine and I could appreciate all the help that I could get!

Feb 12, 2019 in Python by Anirudh
• 2,080 points
2,214 views

1 answer to this question.

0 votes

Hi. It's pretty simple actually. All you need to do is to escape the backslash and you're good to go.

We already know that the \n is the newline character, right? Also, the docstring itself is a string. Check this out:

def foo():
    """
    print "Hello Edureka! \n";
    """
    pass

The docstring might not contain a completely valid Python statement but see that it contains a quoted string with the newline character in it.

Escaping this character is the key to your problem. Hope this helped!

answered Feb 12, 2019 by Nymeria
• 3,560 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

When I create and remove files rapidly on windows using python I get WindowsError (Error 5)

Here's the short answer: disable any antivirus or ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
1,666 views
0 votes
1 answer

Error:UnicodeDecodeError when reading CSV file in Pandas with Python

Hello @kartik, Try this: import pandas as pd df = ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
7,406 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,056 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,477 views
+1 vote
10 answers

How to fix this? ValueError: invalid literal for int() with base 10 error in Python

The following are totally acceptable in python: passing ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,560 points
406,510 views
0 votes
1 answer

How to get textual output when using exceptions in Python?

Hi, the answer is pretty simple.  Without the ...READ MORE

answered Jan 17, 2019 in Python by Nymeria
• 3,560 points
690 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP