![](/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.