Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove backslash continuation character
    text
    copied!<p>I'm trying to parse some code with AST, but I'm having an issue because of backslash continuation character.</p> <p>When I have a continuation character <strong>\</strong>, textwrap will not manage to dedent the code, I would like to know how to get rid of it.</p> <pre><code>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)) </code></pre> <p>I'm adding more details to clarify the question:</p> <p>I have a module nemo.py with the following content:</p> <pre><code>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!" </code></pre> <p>and the main module trying to parse the code:</p> <pre><code>import ast import nemo import inspect import textwrap code = str().join(inspect.getsourcelines(nemo.Foo.bar)[0]) ast.parse(textwrap.dedent(code)) </code></pre> <p>And the traceback:</p> <pre><code>Traceback (most recent call last): File "/Users/kelsolaar/Documents/Development/Research/_BI.py", line 7, in &lt;module&gt; 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 "&lt;unknown&gt;", line 1 def bar(self): ^ IndentationError: unexpected indent </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload