2

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)
hippietrail
  • 15,848
  • 18
  • 99
  • 158
user1091684
  • 101
  • 1
  • 11
  • What code are you using to print the string? Please show your output or interpreter session. – Nayuki Oct 15 '15 at 12:56
  • input() doesn't do any special parsing of escape sequences afaik, it just returns literally what the user typed. What would you want to happen if (for example) the user entered a malformed escape sequence? There is a way to solve this problem here: http://stackoverflow.com/questions/4020539/process-escape-sequences-in-a-string-in-python – Hammerite Oct 15 '15 at 13:00
  • what is the source of such strings? If you need `ast.literal_eval()` or `unicode-escape` encoding; something is broken upstream. `input()` accepts and returns Unicode strings as is. What is `print(ascii(input('Paste Now: ")))`? – jfs Oct 17 '15 at 10:50

1 Answers1

2

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 .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176