简体   繁体   English

在python中将字符串的十六进制字符转换为普通字符

[英]Converting Hexadecimal character of string to normal characters in python

I have a scenario where I have to lodge file path to DB and while fetching the content, I am getting the special characters converted into Hexadecimal values. 我有一种情况,我必须将文件路径存放到DB,并在获取内容时将特殊字符转换为十六进制值。 But with that path, I would not be able to open the files, so I need them in a normal string. 但是使用该路径,我将无法打开文件,因此需要使用普通字符串。 I tried searching a lot but could not get the solution, 我尝试了很多搜索,但找不到解决方案,
for example, I am getting the output as 例如,我得到的输出为

"D:\nhubh\x07oo\x08\x07m"

and my expectation is: 我的期望是:

D:\nhubh\aoo\b\am

Please help. 请帮忙。 Thanks in advance :) 提前致谢 :)

Where was D:\\\\nhubh\\aoo\\b\\am used? D:\\\\nhubh\\aoo\\b\\am在哪里使用?

If it is in a string being fed to, say INSERT , then the backslashes need to be escaped: 如果在要馈入的字符串中,例如INSERT ,则需要转义反斜杠:

D:\\\\nhubh\\aoo\\b\\am

If you are trying use that as a file path, then either of these works: 如果您尝试将其用作文件路径,则可以使用以下任一方法:

D:\\\\nhubh\\aoo\\b\\am
D://nhubh/aoo/b/am

Partial explanation: 部分说明:

The backspace 'character' is hex 08 or represented in some contexts as \\b . 退格“字符”为十六进制08或在某些上下文中表示为\\b Similarly a=alert=07 类似地a = alert = 07

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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