簡體   English   中英

Python:帶有codecs.open的UnicodeEncodeError

[英]Python: UnicodeEncodeError with codecs.open

我正在嘗試使用orgnode.py( 從這里開始 )來解析org文件。 這些文件是英語/波斯語,使用file -i似乎是utf-8編碼的。 但是我在使用makelist函數(其本身使用帶有utf-8的codec.open)時收到此錯誤:

>>> Orgnode.makelist("toread.org")
[**  [[http://www.apa.org/helpcenter/sexual-orientation.aspx][Sexual orientation, homosexuality and bisexuality]]            :ToRead:



Added:[2013-11-06 Wed]
, **  [[http://stackoverflow.com/questions/11384516/how-to-make-all-org-files-under-a-folder-added-in-agenda-list-automatically][emacs - How to make all org-files under a folder added in agenda-list automatically? - Stack Overflow]] 

(setq org-agenda-text-search-extra-files '(agenda-archives "~/org/subdir/textfile1.txt" "~/org/subdir/textfile1.txt"))
Added:[2013-07-23 Tue] 
, Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 63-66: ordinal not in range(128)

該函數返回組織標題列表,但顯示最后一個錯誤(而不是用波斯語寫的最后一項)。 有什么建議我該如何處理這個錯誤?

如回溯告訴您的那樣,異常是由您在Python控制台本身( Orgnode.makelist("toread.org") )上輸入的語句引發的,而不是在評估語句期間調用的函數之一中Orgnode.makelist("toread.org")的。

當解釋器自動轉換語句的返回值以將其顯示回控制台時,這是典型的編碼錯誤。 顯示的文本是將內置的repr()應用於返回值的結果。

這里, makelist結果的repr()是一個unicode對象,解釋器默認情況下會嘗試使用"ascii"編解碼器將其轉換為str

罪魁禍首是Orgnode.__repr__方法( https://github.com/albins/orgnode/blob/master/Orgnode.py#L592 ),該方法返回unicode對象(因為節點內容已通過codecs.open自動解碼),盡管通常要求__repr__方法返回僅包含安全(ASCII)字符的字符串。

這是您可以對Orgnode進行的最小更改,以解決您的問題:

-- a/Orgnode.py
+++ b/Orgnode.py
@@ -612,4 +612,4 @@ class Orgnode(object):
 # following will output the text used to construct the object
         n = n + "\n" + self.body

-        return n
+        return n.encode('utf-8')

如果要使用僅返回ASCII字符的版本,則可以使用'string-escape'作為編解碼器,而不是'utf-8'

這只是一個快速而骯臟的修復程序。 正確的解決方案是重寫適當的__repr__方法,並添加此類缺少的__str____unicode__方法。 (如果有時間的話,我什至可以自己解決這個問題,因為我對使用Python代碼操作Org模式文件非常感興趣)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM