Reputation: 37
I am trying to create a dataframe to store reddit data:
topics_dict = { "title":[],\
"score":[],\
"id":[], "url":[],\ -> Error
"comms_num": [],\
"created": [],\
"body":[]}
Upvotes: 0
Views: 651
Reputation: 366103
It is illegal to have anything between the line-continuation backslash and the newline.1 (See Explicit line joining for details.)
Usually, this problem is pretty obvious:
>>> 'abc' \stuff
'abc' \stuff
^
SyntaxError: unexpected character after line continuation character
… but it's a lot harder to see, even though just as illegal, if all you have is whitespace:
>>> 'abc' \
'abc' \
^
SyntaxError: unexpected character after line continuation character
It can be even worse if you're mixing Windows \r\n
and Unix \n
newlines in the same source. Then, you can have a backslash followed immediately by a \r\n
, which looks perfectly fine—but the \r
is treated as illegal whitespace, not part of the newline, because it's treating your script as \n
-terminated.2
A decent editor will make these problems either hard to create, easy to spot, or both. Pretty much any editor besides Notepad and TextEdit counts as "decent" for these purposes, including lots of free ones for every platform.
The solution is to remove whatever whitespace you have after the \
.
Or, better still, to avoid using backslash continuation when you don't need it—as you don't here, because any expression inside {}
, []
, or ()
is automatically continued, without needing a backslash. (See Implicit line joining for details, but it almost always just works as you'd expect.)
1. Technically, I don't think a backslash followed by something other than a newline is a line continuation at all. So the actual error is just that a backslash on its own is an invalid token. But if so, the tokenizer is being nice by providing a more comprehensible error message here, so let's not complain…
2. I believe this one may no longer be true as of… somewhere around 2.6, or maybe 3.0? See Physical lines. But still, better to use consistent endings than to hope that Python guesses correctly what you were trying to do…
Upvotes: 2
Reputation: 11603
Why not just use:
topics_dict = { "title":[],
"score":[],
"id":[],
"url":[],
"comms_num": [],
"created": [],
"body":[]}
and remove the \
it does exactly the same thing.
This is perfectly valid for a dictionary and is not affected by new lines like strings are (as long as indentation is correct).
Upvotes: 1