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

时间:2018-09-07 09:17:07

标签: python google-vision

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

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


感谢您的所有回答,也许是我为所有人发布代码更容易了。 这是我的代码,基本上,我尝试将GOOGLE Vision的整个json返回值转换为一个json文件,但是并没有成功。

尝试:     代码= request.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除外:     打印(“请求错误”)

谢谢

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我终于通过使用以下代码解决了这个问题。谢谢大家

尝试:     代码= request.post('https://vision.googleapis.com/v1/images:annotate?key='+ GOOGLE_API_KEY,数据=参数,标题=标题)     resultText = json.loads(code.text)     outputFileName = image_path.split('。',1)[0]     使用open(outputFileName +“。json”,“ w”,encoding ='utf8')作为f:         json.dump(resultText,f,sure_ascii = False,indent = 4)     f.close() 除了request.exceptions.ConnectionError:     打印(“请求错误”)

答案 2 :(得分:0)

我假设您的意思是您有一个像\xe4\xb8\x89这样的文字字符串,并且想要将其转换为字符

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

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