-1

This code gives me an error message - obviously because of the \n in the string-list. Error Message: SyntaxError: EOL while scanning string literal

import ast
string = "['Text1', 'Long text\nwith new line...']"
print(ast.literal_eval(string))

Anyone ever dealt with that?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
KatharsisHerbie
  • 115
  • 1
  • 10
  • See [python: SyntaxError: EOL while scanning string literal](https://stackoverflow.com/questions/3561691/python-syntaxerror-eol-while-scanning-string-literal). – Wiktor Stribiżew Sep 04 '17 at 11:03

1 Answers1

2

Use raw string literals:

string = r"['Text1', 'Long text\nwith new line...']"
print(ast.literal_eval(string)[1])

Or manually escape \:

string = "['Text1', 'Long text\\nwith new line...']"
print(ast.literal_eval(string)[1])
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79