2013-08-21 70 views
1

所以我有一個小腳本,我想用一些PDF文件上傳到我的引文站點的選擇題(citeulike.org)蟒蛇機械化文件上傳UnicodeDecode錯誤

事情是它不工作。它這樣做是:

so want to upload /Users/willwade/Dropbox/Papers/price_promoting_643127.pdf to 12589610 
Traceback (most recent call last): 
    File "citeuupload.py", line 167, in <module> 
    cureader.parseUserBibTex() 
    File "citeuupload.py", line 160, in parseUserBibTex 
    self.uploadFileToCitation(b['citeulike-article-id'],self.localpapers+fileorfalse) 
    File "citeuupload.py", line 138, in uploadFileToCitation 
    resp = self.browser.submit() 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_mechanize.py", line 541, in submit 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_mechanize.py", line 203, in open 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_mechanize.py", line 230, in _mech_open 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_opener.py", line 193, in open 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 344, in _open 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 1142, in http_open 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 1115, in do_open 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 955, in request 
    self._send_request(method, url, body, headers) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 989, in _send_request 
    self.endheaders(body) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 951, in endheaders 
    self._send_output(message_body) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 809, in _send_output 
    msg += message_body 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 544: ordinal not in range(128) 

,代碼:

def uploadFileToCitation(self,artid,file): 
    print 'so want to upload', file, ' to ', artid 
    self.browser.open('http://www.citeulike.org/user/'+cUser+'/article/'+artid) 
    self.browser.select_form(name="fileupload_frm") 
    self.browser.form.add_file(open(file, 'rb'), 'application/pdf', file, name='file') 
    try: 
     resp = self.browser.submit() 
     self.wait_for_api_limit() 
    except mechanize.HTTPError, e: 
     print 'error' 
     print e.getcode() 
     print resp.read() 
     exit() 

NB:我可以看到它正在讀取的文件中正確(和它確實存在)。另外請注意,我在其他地方做這個

self.browser = mechanize.Browser() 
    self.browser.set_handle_robots(False) 
    self.browser.addheaders = [ 
     ("User-agent", '[email protected] citeusyncpy/1.0'), 
    ] 

Full code is here

回答

2

嘗試查看此similar question

To clarify, the message is constructed in httplib from the method, URL, headers, etc. If any of these is Unicode, the whole string gets converted to Unicode (I presume this is normal Python behavior). Then if you try to append a UTF-8 string you get the error I described in the original question...

從它看起來與編碼,適當的標題可以解決問題。

你也可以檢查這個issue

+0

謝謝你。要確認[在問題中的補丁](http://bugs.python.org/issue11898)@ ton1c提到解決了問題 – willwade