问题
The following code:
import simplejson,urllib,urllib2
query=[u'नेपाल']
urlbase="http://search.twitter.com/search.json"
values={'q':query[0]}
data=urllib.urlencode(values)
req=urllib2.Request(urlbase,data)
response=urllib2.urlopen(req)
json=simplejson.load(response)
print json
throws exception:
SyntaxError: Non-ASCII character '\xe0' in file ques.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
The code works if query
contains standard ASCII characters. I tried looking at the suggested link but couldn't figure out how to specify encoding for Devanagari characters.
回答1:
You need to add the UTF-8 header to your file to tell the Python interpreter that there are unicode literals. You also have to encode the parameters as UTF-8. Here's a working version:
# -*- coding: utf-8 -*-
import simplejson,urllib,urllib2
query=[u'नेपाल']
urlbase="http://search.twitter.com/search.json"
values={'q':query[0].encode('utf-8')}
data=urllib.urlencode(values)
req=urllib2.Request(urlbase,data)
response=urllib2.urlopen(req)
json=simplejson.load(response)
print json
来源:https://stackoverflow.com/questions/8672057/using-urlencode-for-devanagari-text