input() and literal unicode parsing

流过昼夜 提交于 2021-01-28 02:01:24

问题


Using input() takes a backslash as a literal backslash so I am unable to parse a string input with unicode.

What I mean:

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?

Edit: 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)

回答1:


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))

If the input can contain quotes you can replace double quotes with r'\"' before evaluating using ast.literal_eval .



来源:https://stackoverflow.com/questions/33149120/input-and-literal-unicode-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!