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 8803763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:25:35+00:00 2026-06-14T01:25:35+00:00

I’m trying to parse some code with AST, but I’m having an issue because

  • 0

I’m trying to parse some code with AST, but I’m having an issue because of backslash continuation character.

When I have a continuation character \, textwrap will not manage to dedent the code, I would like to know how to get rid of it.

code = """
    def foo():
        message = "This is a very long message that will probably need to wrap at the end of the line!\n \
And it actually did!"
"""

import textwrap
print textwrap.dedent(code)

import ast
ast.parse(textwrap.dedent(code))

I’m adding more details to clarify the question:

I have a module nemo.py with the following content:

class Foo(object):

    def bar(self):
        message = "This is a very long message that will probably need to wrap at the end of the line!\n \
And it actually did!"

and the main module trying to parse the code:

import ast
import nemo
import inspect
import textwrap

code = str().join(inspect.getsourcelines(nemo.Foo.bar)[0])
ast.parse(textwrap.dedent(code))

And the traceback:

Traceback (most recent call last):
  File "/Users/kelsolaar/Documents/Development/Research/_BI.py", line 7, in <module>
    ast.parse(textwrap.dedent(code))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    def bar(self):
    ^
IndentationError: unexpected indent
  • 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-14T01:25:36+00:00Added an answer on June 14, 2026 at 1:25 am

    This is because you misunderstood what textwrap.dedent() does.

    It only remove any common leading white spaces. In your case there’s no common leading white space, therefore nothing is removed.

    Moreover, what you want is actually \\ instead of \n \ in this case. This is because you actually want what is printed to be parsed. \\ will print only one \ and it’s what you want. \n \ will print a new line within "..." clause which is invalid.

    Now consider this code:

    >>> code = """
        def foo():
            message = "This is a very long message that will probably need to wrap at the end of the line! \\
        And it actually did!"
    """
    
    >>> print textwrap.dedent(code)
    
    def foo():
        message = "This is a very long message that will probably need to wrap at the e
    nd of the line! \
    And it actually did!"
    
    >>> ast.parse(textwrap.dedent(code))
    <_ast.Module object at 0x10e9e5bd0>
    

    In this case there is common leading white spaces, and hence they are removed.


    Edit:

    If you want to get rid of the \ all together, you can consider using """My sentence""" for message in def bar.

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

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.