如何将python代码字符转换为人类可读代码

问题描述 投票:1回答:3

我目前在python中使用Google Vision API来检测图像中的中文字符,但我发现google将返回python源代码(例如\ xe7 \ x80 \ x86 \ xe7 \ xab \ x91)而不是某些人类可读的字符串。

如何将其转换为utf-8格式的人类可读文本?


谢谢你的所有答案,可能是我发布我的代码更容易为你们所有人。这是我的代码,基本上我尝试从GOOGLE Vision转换整个json返回并保存在json文件中,但是,它没有成功。

尝试: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()

除了requests.exceptions.ConnectionError:print('请求错误')

谢谢

python google-vision
3个回答
1
投票
t = '\xe7\x80\x86\xe7\xab\x91'
t = unicode('\xe7\x80\x86\xe7\xab\x91', 'utf8')
# Output: 瀆竑

有关here中Unicode的更多详细信息。


1
投票

我终于使用下面的代码解决了这个问题。谢谢大家

尝试: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()除了requests.exceptions.ConnectionError:print('请求错误')


0
投票

我假设你的意思是你有像\xe4\xb8\x89这样的文字字符串,你想把它转换成字符

很奇怪,没有一种简单的方法可以做到这一点。我能想到的最好的是:

s = '\\xe4\\xb8\\x89'
print(bytes.fromhex(s.replace('\\x', '')).decode('utf-8')) # prints 三
© www.soinside.com 2019 - 2024. All rights reserved.