-----------------------------------------------
A = 0x00000000
B = 0x00000001
C = 0x00000002
D = 0x00000003
E = 0x00000004
F = 0x00000005
ABCD = 0xFFFF
AB = 0x5AFF
A = 0x00000000
B = 0x00000001
C = 0x00000002
D = 0x00000003
E = 0x00000004
F = 0x00000005
A = 0x00000000
B = 0x00000001
C = 0x00000002
D = 0x00000003
E = 0x00000004
F = 0x00000005
input_to_format.py
1.0 处理单个固定文件
# coding=utf-8
if __name__ == '__main__':
input_file = 'input_msg.txt'
# 读出文件内容
file_object = open(input_file, 'r')
file_content = file_object.readlines()
file_object.close()
# 删除每行的中'='、'0x'、'\n'、' '
buff = ''
for i in range(0, len(file_content)):
del_equal = file_content[i].split('=')
del_x = del_equal[1].replace('0x', '')
del_enter = del_x.replace('\n', '')
del_space = del_enter.replace(' ', '')
buff += del_space
# 格式化,每行16个字节
output_content = ''
output_content += buff[0]
for i in range(1, len(buff)):
if (i%32 == 0):
output_content += '\n'
elif (i%2 == 0):
output_content += ' '
output_content += buff[i]
# 以可写属性打开文件
output_file = 'format_msg.txt'
file_object = open(output_file, 'w')
file_object.write(output_content)
file_object.close()
2.0 递归处理当前目录下的所有文件
-----------------------------------------------
# coding=utf-8
import os
def format_file(input_file):
# 读出文件内容
file_object = open(input_file, 'r')
file_content = file_object.readlines()
file_object.close()
# 删除每行的中'='、'0x'、'\n'、' '
buff = ''
for i in range(0, len(file_content)):
split_by_flag = file_content[i].split('=')
del_content = split_by_flag[1].replace('0x', '')
del_content = del_content.replace('0X', '')
del_content = del_content.replace('\n', '')
del_content = del_content.replace(' ', '')
buff += del_content
# 格式化,每行16个字节
output_content = ''
output_content += buff[0]
for i in range(1, len(buff)):
if (i%32 == 0):
output_content += '\n'
elif (i%2 == 0):
output_content += ' '
output_content += buff[i]
# 以可写属性打开文件
output_file = input_file.replace('.msg', '.txt')
file_object = open(output_file, 'w')
file_object.write(output_content)
file_object.close()
def excuepath(workdir):
os.chdir(workdir)
cwd = os.getcwd()
dirs = os.listdir(cwd)
for tmp in dirs:
path=os.path.join(cwd,tmp)
if os.path.isfile(path):
if os.path.splitext(path)[1] == '.msg':
print path
format_file(path)
elif os.path.isdir(path):
excuepath(path)
if __name__ == '__main__':
cwd = os.getcwd()
excuepath(cwd)
3.0 递归方式更加简洁;解决.msg文件中的空行问题
-----------------------------------------------
# coding=utf-8
import os
def format_file(input_file):
# 读出文件内容
file_object = open(input_file, 'r')
file_content = file_object.readlines()
file_object.close()
# 删除每行的中'='; '0x'; '0X'; '\n'; ' '
buff = ''
for i in range(0, len(file_content)):
if (file_content[i].find('=') != -1):
split_by_flag = file_content[i].split('=')
del_content = split_by_flag[1].replace('0x', '')
del_content = del_content.replace('0X', '')
del_content = del_content.replace('\n', '')
del_content = del_content.replace(' ', '')
buff += del_content
# 格式化,每行16个字节
output_content = ''
output_content += buff[0]
for i in range(1, len(buff)):
if (i%32 == 0):
output_content += '\n'
elif (i%2 == 0):
output_content += ' '
output_content += buff[i]
# 以可写属性打开文件
output_file = input_file.replace('.msg', '.txt')
file_object = open(output_file, 'w')
file_object.write(output_content)
file_object.close()
if __name__ == '__main__':
cwd = os.getcwd()
for (path, dirs, files) in os.walk(cwd):
for filename in files:
if os.path.splitext(filename)[1] == '.msg':
filename = os.path.join(path,filename)
print filename
format_file(filename)
format_msg.txt
-----------------------------------------------
00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 03
00 00 00 04 00 00 00 05 FF FF 5A FF 00 00 00 00
00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 04
00 00 00 05 00 00 00 00 00 00 00 01 00 00 00 02
00 00 00 03 00 00 00 04 00 00 00 05