python3 读入字符串类型,输出16进制文件

本文介绍了一个简单的Python脚本,用于从输入文件中读取16进制数据,去除空格,并将其转换为字节写入输出文件。此过程涉及到文件操作和16进制数据处理。

输入如下,只有1行,考虑到做结束,需要在末尾加换行。

AA 44 12 1C 8C 00 00 02 8C 08 00 00 61 A0 02 08 38 

输出直接为16进制,考虑到空格,做了计数器来跳空格。
在这里插入图片描述

import time
print("hello world")


#infile = open("201904251.log","r");
infile = open("1.log","r");
outfile = open("out.log","wb");
_log = open("log.log","w");
a = infile.read(1);
_combine = "";
i=0;
while a != '\n':
    if ' ' == a:
        a = infile.read(1);
        continue;
    else:
        _combine = _combine + a;
        i = i+1;
        if 2 == i :
            i = 0;
            #print(_combine);
            #_log.write(" "+_combine);
            outfile.write(bytes.fromhex(_combine));
            _combine = "";
    a = infile.read(1);

infile.close();
outfile.close();
_log.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值