問題描述
如何在 python 中將原始 unicode 轉換為 utf8‑unicode? (How to convert raw unicode to utf8‑unicode in python?)
第一次來這裡,我會盡力解釋我的問題。
我正在使用 Maya 中的 python2.7。我得到了一個使用 Maya API 導入的字符串(稱為屬性)“attr”,如下所示:
print(attr)
print(type(attr))
>> Générique
>> <type 'unicode'>
我需要將其轉換為 utf‑8 可讀格式,然後才能繼續使用我的工作。基本上我需要能夠做到這一點:
print(attr)
print(type(attr))
>>Générique
>><type 'unicode'>
我已經嘗試了 attr.encode / attr.decode 的多種組合,但我無法真正掌握我應該做什麼。最困擾我的是,當我嘗試在代碼中手動鍵入變量時,您實際上可以得到:
attr = 'Générique'
print(type(attr))
attr = attr.decode('utf‑8')
print(attr)
print(type(attr))
>><type 'str'>
>>Générique
>><type 'unicode'>
所以我知道我最初應該將 'attr' 轉換為 str 類型,但我可以不要在不丟失信息的情況下這樣做。
有任何想法嗎 ?請?
編輯:由snakecharmerb(和ftfy)解決。非常感謝。這篇文章下的兩種解決方案。
參考解法
方法 1:
SOLVED :
I found out about the module FTFY. Was a bit of a hassle to make pip work with Maya but it's all fine and done. To anyone with the same issue: make pip work with maya: https://forums.autodesk.com/t5/maya‑programming/can‑i‑use‑pip‑in‑maya‑script‑editor/td‑p/7638107 (you'll need to run admin cmd or it won't install)
grab ftfy (version below 5 was compatible with python2.7): pip install ftfy==4.4.3
my unclean code looks like this :
from __future__ import unicode_literals
import pymel.core as pm
import maya.cmds as cmds
import maya.utils
import unicodedata
import StringIO
import codecs
import sys
import re
from ftfy import fix_text
attr = cmds.getAttr(*objectName*)
attr = fix_text(attr)
print(attr)
方法 2:
What you have is text that was originally UTF‑8 but decoded with an 8‑bit encoding, likely latin‑1 or cp1252. To fix the text you need to encode to the 8‑bit encoding to get the UTF‑8 bytes and then decode.
>>> u = u'Générique'
>>> fixed = u.encode('latin‑1').decode('utf‑8')
>>> print fixed
Générique
(by gargam、gargam、snakecharmerb)