SyntaxError with nested quotes in Python

96 Views Asked by At

I am using a Twitter XAuth Python library and I keep getting an Invaild Syntax Error. I am still learning python but I couldn't find the solution when I was looking. Here is the code giving the error. Any help would be great.

req = urllib2.Request(post_url, data = urllib.urlencode(params))
req.add_data(urllib.urlencode({'status' : message}))
req.add_header('Authorization', 'OAuth %s' % ', '.join(
  ['%s='%s'' % (x, urllib.quote(params[x], '')) for x in params]))

Thank you.

1

There are 1 best solutions below

1
Arya McCarthy On BEST ANSWER

You have two random parentheses at the end of the line, and you should use double quotes around your string—Stack Overflow's syntax highlighting should make that clear. Next time, include the full stack trace.

EDIT: with the updated question, the parentheses are no longer an issue.

req.add_header('Authorization', 'OAuth %s' % ', '.join(
    ["%s='%s'" % (x, urllib.quote(params[x], '')) for x in params]))

See how everything that's part of the string is red in SO's syntax highlighting?