![](/img/trans.png)
[英]Comprehensive character replacement module in python for non-unicode and non-ascii for HTML
[英]Unicode string to backslash-encoded non-unicode 7-bit ASCII in python?
環境: Python 2.6 ... Python 2.higher than than-6
我有正確u''
u''UTF-8字符串,需要將其轉換為標准Python 2.6格式ASCII字符串中的ASCII編碼格式。 像這樣:
def conversionSolution(utf8StringInput):
{
...
return(asciiStringResult)
}
utf8string = u'\u5f00\u80c3\u83dc'
asciistring = conversionSolution(utf8string)
print asciistring
填入...
后,上面的內容會打印出來...
而不是 ...
我要強調,我不希望UTF-8在這里; 我特別要求使用0-127編碼的ASCII反斜杠數據,然后我可以嚴格將其作為7位ASCII處理。
您可以調用.encode('unicode-escape')
來執行此操作。
話雖這么說,您正在談論之后要處理該字符串。 之后,使用該字符串沒有太大用處。 例如。 如果您對其進行切片,則可以在這些轉義序列的中間進行切片。 大小寫折疊當然是行不通的,等等。如果您需要操作該字符串,則應將其保留為unicode字符串。
def conversionSolution(utf8StringInput):
return repr(utf8StringInput)[2:][:-1]
utf8string = u'\u5f00\u80c3\u83dc'
asciistring = conversionSolution(utf8string)
print asciistring
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.