input()和文字unicode解析 [英] input() and literal unicode parsing
问题描述
使用 input()
将反斜杠用作文字反斜杠,因此我无法使用unicode解析字符串输入.
Using input()
takes a backslash as a literal backslash so I am unable to parse a string input with unicode.
我的意思是
将类似"\ uXXXX \ uXXXX \ uXXXX"
的字符串粘贴到 input()
调用中,将被解释为"\\ uXXXX \\ uXXXX \\ uXXXX"
,但我希望它以单个字符而不是两个单独的字符的形式读取 \ u
.
Pasting a string like "\uXXXX\uXXXX\uXXXX"
into an input()
call will become interpreted as "\\uXXXX\\uXXXX\\uXXXX"
but I want it read \u
as a single character instead of two separate characters.
有人知道如何或可能实现它吗?
Does anyone know how or if possible to make it happen?
我正在接受上述输入并将其转换为如下的ascii.
I am taking input as above and converting it to ascii such as below..
import unicodedata
def Reveal(unicodeSol):
solution = unicodedata.normalize('NFKD', unicodeSol).encode('ascii', 'ignore')
print(solution)
while(True):
UserInput = input("Paste Now: ")
Reveal(UserInput)
根据我标记的答案,正确的解决方案是:
Per the answer I marked, a correct solution would be:
import unicodedata
import ast
def Reveal(unicodeSol):
solution = unicodedata.normalize('NFKD', unicodeSol).encode('ascii', 'ignore')
print(solution)
while(True):
UserInput = ast.literal_eval('"{}"'.format(input("Paste Now: ")))
Reveal(UserInput)
推荐答案
如果可以确定输入中不包含引号,则可以通过在两端加上引号将输入转换为字符串文字表示形式,然后使用 ast.literal_eval()
将其评估为字符串.示例-
If you can be sure that input would not contain quotes, you can convert the input into a string literal representation, by adding quotes in both ends , and then use ast.literal_eval()
to evaluate it into a string. Example -
import ast
inp = input("Input : ")
res = ast.literal_eval('"{}"'.format(inp))
如果输入中可以包含引号,则可以在使用ast.literal_eval进行评估之前,将双引号替换为 r'\'
.
If the input can contain quotes you can replace double quotes with r'\"'
before evaluating using ast.literal_eval .
这篇关于input()和文字unicode解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!