2010-10-06 14 views
5

मैं एक स्ट्रिंग है s = 'Chocolate Moelleux-M\xe8re' कहना जब मैं कर रहा हूँ:पायथन में एक गैर यूनिकोड चरित्र को डीकोड कैसे करें?

In [14]: unicode(s) 
--------------------------------------------------------------------------- 
UnicodeDecodeError      Traceback (most recent call last) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 20: ordinal not in range(128) 

इसी तरह जब मैं s.decode() का उपयोग कर यह एक ही त्रुटि देता है द्वारा इस डिकोड करने के लिए कोशिश कर रहा हूँ।

In [13]: s.decode() 
--------------------------------------------------------------------------- 
UnicodeDecodeError      Traceback (most recent call last) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 20: ordinal not in range(128) 

ऐसी स्ट्रिंग को यूनिकोड में कैसे डीकोड करें।

उत्तर

10

मुझे इस समस्या का सामना कई बार करना पड़ा है। समस्या जो मैंने अलग-अलग एन्कोडिंग योजनाओं में तारों को शामिल किया था। तो मैंने अलग-अलग एन्कोडिंग की कुछ विशेषताओं के आधार पर एक स्ट्रिंग को व्यवस्थित करने के लिए एक विधि लिखी।

def decode_heuristically(string, enc = None, denc = sys.getdefaultencoding()): 
    """ 
    Try to interpret 'string' using several possible encodings. 
    @input : string, encode type. 
    @output: a list [decoded_string, flag_decoded, encoding] 
    """ 
    if isinstance(string, unicode): return string, 0, "utf-8" 
    try: 
     new_string = unicode(string, "ascii") 
     return string, 0, "ascii" 
    except UnicodeError: 
     encodings = ["utf-8","iso-8859-1","cp1252","iso-8859-15"] 

     if denc != "ascii": encodings.insert(0, denc) 

     if enc: encodings.insert(0, enc) 

     for enc in encodings: 
      if (enc in ("iso-8859-15", "iso-8859-1") and 
       re.search(r"[\x80-\x9f]", string) is not None): 
       continue 

      if (enc in ("iso-8859-1", "cp1252") and 
       re.search(r"[\xa4\xa6\xa8\xb4\xb8\xbc-\xbe]", string)\ 
       is not None): 
       continue 

      try: 
       new_string = unicode(string, enc) 
      except UnicodeError: 
       pass 
      else: 
       if new_string.encode(enc) == string: 
        return new_string, 0, enc 

     # If unable to decode,doing force decoding i.e.neglecting those chars. 
     output = [(unicode(string, enc, "ignore"), enc) for enc in encodings] 
     output = [(len(new_string[0]), new_string) for new_string in output] 
     output.sort() 
     new_string, enc = output[-1][1] 
     return new_string, 1, enc 

इस में जोड़ने के लिए इस लिंक पर क्यों एन्कोडिंग आदि पर एक अच्छी प्रतिक्रिया देता है - Why we need sys.setdefaultencoging in py script

4

आपको अपने एन्कोडिंग s.decode को बताना होगा। आपके मामले में s.decode('latin-1') फिटिंग लगता है।

+0

यह मुझे सब स्थिति में मदद करने के लिए जा रहा है? क्या कोई सामान्यीकृत समाधान है? – user12345

+0

क्या हम मूल स्ट्रिंग से, मेरे उदाहरण में 'x' जैसे उन वर्णों को हटा सकते हैं। – user12345

+0

@alis: आप एन्कोडिंग अनुमान लगाने के लिए chardet (http://chardet.feedparser.org/) का उपयोग कर सकते हैं। – johnbaum

संबंधित मुद्दे