环境:Python2.6. Python 2.高于-6
我有正确的u''
UTF-8字符串,需要用标准的Python2.6-ish ASCII字符串转换为ASCII编码格式。就像这样:
def conversionSolution(utf8StringInput):
{
...
return(asciiStringResult)
}
utf8string = u'\u5f00\u80c3\u83dc'
asciistring = conversionSolution(utf8string)
print asciistring
当...
填好后,上面的内容会打印出来.
U5f00\u80c3\u83dc
而不是..。
开胃菜
让我强调一下,我不想在这里使用UTF-8;我特别需要0-127编码的ASCII反斜杠数据,然后我可以作为7位ASCII严格地操作这些数据。
发布于 2016-02-01 20:50:18
def conversionSolution(utf8StringInput):
return repr(utf8StringInput)[2:][:-1]
utf8string = u'\u5f00\u80c3\u83dc'
asciistring = conversionSolution(utf8string)
print asciistring
发布于 2016-02-01 20:49:31
你可以叫.encode('unicode-escape')
来做这件事。
话虽如此,你说的是事后操纵那根绳子。没有什么有用的,你可以用这个字符串之后。例如:如果你把它切片,你可以在这些转义序列的中间切片。当然,大小写折叠不起作用,等等。如果您需要操作该字符串,您应该将其保持为unicode字符串。
https://stackoverflow.com/questions/35139905
复制相似问题