pythontty 2018-10-26
实现目标:把文件1中数据如:B4A6C0ED69 处理后放入文件2:0XB4, 0XA6, 0XC0, 0XED, 0X69
V1.0代码如下(后续继续优化):
#!/usr/bin/env python # -*- coding:utf-8 -*- from sys import argv script,first = argv buf = [] tmp = [] #读取待处理文件全部内容 并存到buf中 with open(first, 'r') as f: buf = f.read() f.closed #对buf中内容,进行每隔2个字符取出,并以", 0X"连接,最后在头部加上'0X' for i in range(0,len(buf),2): tmp.append(buf[i:i+2]) hex_temp = ", 0X".join(tmp) hex_buf = '%s%s' %('0X', hex_temp) #把处理后的hex数据写入到hex.txt文件中 with open("hex.txt", 'w') as out: out.write(hex_buf) out.close()
执行过程(注意用命令行输入文件1参数的形式):
输出结果: