我的 Python 版本是 2.4.3,我想在代码下面运行,但我收到 str 格式错误。这是错误:
[xxx@bdm ~/python]$ ./run.sh
rfb.pcap
RFB\x20[0-9][0-9][0-9][.][0-9][0-9][0-9]\x0a
Traceback (most recent call last):
File "py_regex.py", line 14, in ?
print " ".join("{0:02x}".format(ord(c)) for c in match)
File "py_regex.py", line 14, in <generator expression>
print " ".join("{0:02x}".format(ord(c)) for c in match)
AttributeError: 'str' object has no attribute 'format'
这是我的代码:
#!/usr/bin/env python
import re
import sys
file = sys.argv[1]
exp = sys.argv[2]
print file
print exp
myfile = open(file, "r")
try:
data = myfile.read()
p = re.compile(exp)
matches = p.findall(data)
for match in matches:
print " ".join("{0:02x}".format(ord(c)) for c in match)
最后:myfile.close()