简体   繁体   中英

How to convert python code character to human readable code

I am currently using Google Vision API in python to detect Chinese character in an image, but I found google will return python source code (Such as \\xe7\\x80\\x86\\xe7\\xab\\x91) instead of some human-readable string.

How can I convert it to human-readable text with utf-8 format?


Thanks all of your answer, may be I post my code is more easily for all of you. Here is my code, basically I try to convert the whole json return from GOOGLE Vision and save in a json file, however, it hasn't success.

try: code = requests.post(' https://vision.googleapis.com/v1/images:annotate?key= '+GOOGLE_API_KEY, data=params,headers=headers)

resultText = code.text.encode("utf-8")
outputFileName = image_path.split('.',1)[0]
outputDataFile = open(outputFileName+".json", "w")
outputDataFile.write(json.dumps(resultText))
outputDataFile.close()

except requests.exceptions.ConnectionError: print('Request error')

Thank you

t = '\xe7\x80\x86\xe7\xab\x91'
t = unicode('\xe7\x80\x86\xe7\xab\x91', 'utf8')
# Output: 瀆竑

More detailed information about Unicode in here .

I finally solve this by using the below code. Thanks all of you

try: code = requests.post(' https://vision.googleapis.com/v1/images:annotate?key= '+GOOGLE_API_KEY, data=params,headers=headers) resultText = json.loads(code.text) outputFileName = image_path.split('.',1)[0] with open(outputFileName+".json", "w", encoding='utf8') as f: json.dump(resultText, f, ensure_ascii=False,indent=4) f.close() except requests.exceptions.ConnectionError: print('Request error')

I assume you mean you have a literal string like \\xe4\\xb8\\x89 and you want to convert this into the character .

It's very strange that there isn't a straightforward way to do this. The best i can come up with is:

s = '\\xe4\\xb8\\x89'
print(bytes.fromhex(s.replace('\\x', '')).decode('utf-8')) # prints 三

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM