Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何避免;ValueError:无效\x转义“;这种特殊情况下的错误?_Python_Syntax Error_Wildcard - Fatal编程技术网

Python 如何避免;ValueError:无效\x转义“;这种特殊情况下的错误?

Python 如何避免;ValueError:无效\x转义“;这种特殊情况下的错误?,python,syntax-error,wildcard,Python,Syntax Error,Wildcard,显然,通配符%x不能识别为字节十六进制值,因此我得到错误“ValueError:invalid\x escape” 如何避免这种情况?我不熟悉python xrange(0,长度)中的i的: 如果i%2==0: tempArray.append(解包(“B”,有效负载_原始[x])[0]^key) x+=1 其他: randomByte=random.randint(65,90) tempArray.append(随机字节) 对于范围(0,len(tempArray))中的i: tempArra

显然,通配符%x不能识别为字节十六进制值,因此我得到错误“ValueError:invalid\x escape”

如何避免这种情况?我不熟悉python

xrange(0,长度)中的i的
:
如果i%2==0:
tempArray.append(解包(“B”,有效负载_原始[x])[0]^key)
x+=1
其他:
randomByte=random.randint(65,90)
tempArray.append(随机字节)
对于范围(0,len(tempArray))中的i:
tempArray[i]=“\x%x”%tempArray[i]
对于范围内的i(0,len(tempArray),15):
outArray.append(“\n'”+”.join(tempArray[i:i+15])+“”)
outArray=”“.加入(outArray)
devide=“i%2;”
open_structure=open(structure).read()
代码=开放结构%(junkA、outArray、junkB、key、length、devide)
b、 写(代码)
b、 刷新()
chr()
将为0到255之间的值提供bytestring

>>> chr(0xd3)
'\xd3'

伊格纳西奥的回答是正确的,但原力也有阴暗面:

>>> your_string = r'\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21'
>>> your_string.decode('string-escape')
'Hello, World!'

因此,您可以使用原始文字(r'\x'而不是'\x')修复问题,并使用
str.decode
方法将转义转换为字符。

您希望使用该行完成什么
\x
只会在字符串文本中执行一些有用的操作;不能以这种方式使用字符串格式。