简体   繁体   中英

Remove backslash continuation character

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

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 .

For the second part of the question I the following simple replace covers my needs: code.replace("\\\\n", str())

import ast
import nemo
import inspect
import textwrap

code = str().join(inspect.getsourcelines(nemo.Foo.bar)[0])
code.replace("\\\n", str())
ast.parse(textwrap.dedent(code))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM