Python 为什么可以';我不能逃避特殊角色吗
我的代码是 hexviewer.py:Python 为什么可以';我不能逃避特殊角色吗,python,Python,我的代码是 hexviewer.py: def hex_viewer(filename): data = open(filename,"rb").read() listofhex = map(lambda x: b"%02X" % ord(x),data) listofchar = map(lambda x: "%s" % x,data) print "\n".join(["{0}\t{1}".format(" ".join(listofhex[x:x+10]),
def hex_viewer(filename):
data = open(filename,"rb").read()
listofhex = map(lambda x: b"%02X" % ord(x),data)
listofchar = map(lambda x: "%s" % x,data)
print "\n".join(["{0}\t{1}".format(" ".join(listofhex[x:x+10])," ".join(listofchar[x:x+10]) ) for x in xrange(0,len(listofhex),10)] )
hex_viewer("hexviewer.txt")
hello
world
和我的源文件
hexviewer.txt:
def hex_viewer(filename):
data = open(filename,"rb").read()
listofhex = map(lambda x: b"%02X" % ord(x),data)
listofchar = map(lambda x: "%s" % x,data)
print "\n".join(["{0}\t{1}".format(" ".join(listofhex[x:x+10])," ".join(listofchar[x:x+10]) ) for x in xrange(0,len(listofhex),10)] )
hex_viewer("hexviewer.txt")
hello
world
我认为在中国的产出应该是
68 65 6C 6C 6F 20 0D 0A 77 6F h e l l o \r \n w o
72 6C 64 r l d
但产出是有限的
68 65 6C 6C 6F 20 0D 0A 77 6F h e l l o
w o
72 6C 64 r l d
我做错了什么
编辑:
很抱歉,我尝试编辑我的代码
listofchar = map(lambda x: "%s" % x,data)
这里
但产出是有限的`
68 65 6C 6C 6F 20 0D 0A 77 6F 'h' 'e' 'l' 'l' 'o' ' ' '
' '
' 'w' 'o'
72 6C 64 'r' 'l' 'd'
我试着
import re
listofchar = map(lambda x: re.escape(r"%s") % x,data)
listofchar = map(lambda x: r"%s".replace("\\","\\\\") % x,data)
输出
68 65 6C 6C 6F 20 0D 0A 77 6F \h \e \l \l \o \ \
\
\w \o
72 6C 64 \r \l \d
我试着
import re
listofchar = map(lambda x: re.escape(r"%s") % x,data)
listofchar = map(lambda x: r"%s".replace("\\","\\\\") % x,data)
输出为
68 65 6C 6C 6F 20 0D 0A 77 6F h e l l o
w o
72 6C 64 r l d
还有很多方法,但我忘了
很抱歉,T^T您需要的是一种不使用普通字符的方法,而只是对特殊字符进行编码
listofchar = map(lambda x: x.encode('unicode-escape'), data)
请注意,您可以使用列表理解而不是映射,这是首选方法
listofchar = [x.encode('unicode-escape') for x in data]
每次打印一个字符时,您希望这两个字符\n来自哪里?你在哪里做你在标题中提到的逃跑?非常感谢:)@Mark Ransom