python 读写文件

input_msg.txt
-----------------------------------------------
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值