2014-09-02 107 views
0

我正在使用python for visual studio 2012(python版本2.7.2),我有一本俄語字典('УТВЕРЖДЕНО')和普通話符號('項目名稱' ),當我使用Python寫這些到ArcGIS地圖文件,所有我的文檔中看到的是這些「????」,我已經使用這個代碼:Python將俄語,普通話字符寫入ArcGIS地圖文檔

#!/usr/bin/python 
# -*- coding: iso8859_5 -*- 

還是那些問號露面,是有辦法處理這個?

+0

你不需要使用utf-8嗎? – Ashalynd 2014-09-02 17:41:39

+0

我也使用過,仍然有問號出現 – GBh 2014-09-02 17:44:25

+0

我可以使用編解碼器來編碼字符嗎? – GBh 2014-09-03 21:05:58

回答

0

使用codecs模塊,並確保您要寫入文件的內容是unicode。 (有關更多詳細信息,請參閱Kumar McMillan的talk from PyCon 2008)。

下面是如何使用codecs模塊將俄文和普通話中文字符寫入文件的最簡單示例。

#! /usr/bin/env python 
# -*- coding: utf-8 -*- 

import codecs 

def to_unicode_or_bust(obj, encoding='utf-8'): 
    ## by Kumar McMillan (http://farmdev.com/talks/unicode/) 
    if isinstance(obj, basestring): 
     if not isinstance(obj, unicode): 
      obj = unicode(obj, encoding) 
    return obj 

mystring = 'String input: УТВЕРЖДЕНО, 項目名稱\n' 

mystring_unicode = u'Unicode input: УТВЕРЖДЕНО, 項目名稱\n' 

with codecs.open("filename.txt", 'a', encoding='utf-8') as stream: 
    stream.write(to_unicode_or_bust(mystring)) 
    stream.write(to_unicode_or_bust(mystring_unicode))