मैं चाहता हूं कि मेरा फ़ंक्शन एक तर्क लेने के लिए जो एक यूनिकोड ऑब्जेक्ट या एक यूटीएफ -8 एन्कोडेड स्ट्रिंग हो। मेरे फ़ंक्शन के अंदर, मैं तर्क को यूनिकोड में कनवर्ट करना चाहता हूं। मेरे पास ऐसा कुछ है:डीकोडिंग अगर यह यूनिकोड नहीं है
def myfunction(text):
if not isinstance(text, unicode):
text = unicode(text, 'utf-8')
...
क्या इंस्टेंसेंस के उपयोग से बचना संभव है? मैं कुछ और बतख-टाइपिंग दोस्ताना ढूंढ रहा था।
डीकोडिंग के साथ अपने प्रयोगों के दौरान, मैंने पाइथन के कई अजीब व्यवहारों में भाग लिया है। उदाहरण के लिए:
>>> u'hello'.decode('utf-8')
u'hello'
>>> u'cer\xf3n'.decode('utf-8')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in po
sition 3: ordinal not in range(128)
या
>>> u'hello'.decode('utf-8')
u'hello' 12:11
>>> unicode(u'hello', 'utf-8')
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: decoding Unicode is not supported
वैसे। मैं पाइथन 2.6
आप इस सवाल पर एक नज़र लेने के लिए चाहते हो सकता है: - (http://stackoverflow.com/questions/368805/) – tzot