如何让Python循环遍历文件并将十六进制转换为ascii?问题是,当Python循环遍历文件时,它会看到\n
并且不解析文件。
import binascii
fo = open('test.doc', 'r')
print fo.readlines()
dataFormatHex = binascii.a2b_hex ("%s" %fo)
output = ""
for char in dataFormatHex:
if char in string.printable: output += char
else: output += "."
print "\n" + output
错误跟踪是:
Traceback (most recent call last):
File "C:\Python27\hex2ascii-looper.py", line 7, in <module>
dataFormatHex = binascii.a2b_hex ("%s" %fo)
TypeError: Non-hexadecimal digit found
以下是test.doc中的内容:
140000003700000027000000260000006D0000008E000000040000002E000000000000001D00000003000000410000004D0000005800000094000000880000005E0000004F00000040000000360000007B000000660000005A00000042000000300000002F0000004300000048000000050000000C0000004900000044000000220000002D000000180000003800000011000000010000001500000009000000640000008100000020000000700000006C00000087000000650000000F000000610000005D0000001F000000210000001900000079000000690000008A0000008D0000002A0000008C00000073000000330000003B000000680000006F000000800000000E000000020000004A0000005900000039000000740000001C00000012000000060000001E000000830000006B0000006A000000750000000A0000007A00000023000000550000007E000000340000001B0000001700000084000000520000007700000092000000470000009300000076000000710000004600000060000000720000002B000000530000001A000000900000007D00000016000000670000008B00000063000000450000002C00000035000000070000005B00000050000000910000002900000086000000310000003A0000000D0000007F000000560000002400000054000000890000006E000 00085000000950000008F0000004C0000007800000062000000570000003D0000000B00000010000000130000002500000032000000820000004E0000005C0000003F000000080000004B000000280000005F0000003C000000510000003E000000FFFFFFFF 310032002D00310038002D00310034005F004D0065007800690063006F0020006D0061006300680069006E006500730020006F007500740020006F006600200064006100740065002D006E00650077002E0078006C00730078000000D00032000000000000000000000031322D31382D31345F4D657869636F206D616368696E6573206F7574206F6620646174652D6E65772E786C73782E6C6E6B00900008000400EFBE00000000000000002A00000000000000000000000000000000000000000000000000310032002D00310038002D00310034005F004D0065007800690063006F0020006D0061006300680069006E006500730020006F007500740020006F006600200064006100740065002D006E00650077002E0078006C00730078002E006C006E006B00000040000000 6300620074006E005F0070007900740068006F006E005F003000350020002D002000530074006100740065006D0065006E00740073002C00200044006F00630075006D0065006E0074006100740069006F006E002C00200061006E0064002000480065006C0070002E0066006C0076000000F2003200000000000000000000006362746E5F707974686F6E5F3035202D2053746174656D656E74732C20446F63756D656E746174696F6E2C20616E642048656C702E666C762E6C6E6B0000A60008000400EFBE00000000000000002A000000000000000000000000000000000000000000000000006300620074006E005F0070007900740068006F006E005F003000350020002D002000530074006100740065006D0065006E00740073002C00200044006F00630075006D0065006E0074006100740069006F006E002C00200061006E0064002000480065006C0070002E0066006C0076002E006C006E006B0000004C000000
答案 0 :(得分:0)
只需在字符串上使用.strip()
,就会删除尾随的\n
。
import binascii
f = open(yourFile)
for line in f.readlines():
print(binascii.a2b_hex("%s" % (line.strip()))
f.close()