昨天处理一批串口数据文本,有许多模式,对应了很多数据,数据是每十个为一组,全部以48开头。为了省事,可以通过python进行文本处理,将数据全部按48换行。

原始数据如:48 54 05 00 03 00 66 18 00 22 48 54 05 00 03 00 66 18 00 22 48 54 05 00 03 00 66 18 00 22 48 54 04 00 00 00 00 00 00 A2 48 54 05 00 03 00 66 18 00 22 48 54 05 00 03 00 66 18 00 22 48 54 05 00 03 00 66 18 00 22 48 54 04 00 02 00 66 18 00 22
保存了很多文件。

1、对文件夹内所以文件进行处理,适合大批量文件

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#filename:exchange_newline_all.py

import re,os


def newline(file_name):#定义转换函数
    f = open(file_name,'r')
    content = f.readlines()
    f.close()

    f = open(file_name,'w')
    for line in content:
        f.writelines(re.sub('48','\n48',line))#在48之前换行替换

    f.close()


while True:
    path = raw_input('INPUT THE FILEPATH(eg F:\sscom\dir):')#输入待处理文件夹
    print path
    l = os.listdir(r'%s' % path)#遍历文件夹内文件
    num = len(l)
    for i in range(0,num):#逐一处理每个文件
        file_x = r'%s\%s' % (path, l[i])
        newline(file_x)
        print "FINISHED,OK"

输出:

>>> ================================ RESTART ================================
>>> 
INPUT THE FILEPATH(eg F:\sscom\dir):F:\tmp\1307\sscom\tmp2\mode_test
F:\tmp\1307\sscom\tmp2\mode_test
FINISHED,OK
FINISHED,OK
FINISHED,OK
FINISHED,OK
FINISHED,OK
FINISHED,OK
FINISHED,OK
FINISHED,OK
FINISHED,OK
INPUT THE FILEPATH(eg F:\sscom\dir):

运行处理后文件内容为:

48 54 05 00 03 00 66 18 00 22
48 54 05 00 03 00 66 18 00 22
48 54 05 00 03 00 66 18 00 22
48 54 04 00 00 00 00 00 00 A2
48 54 05 00 03 00 66 18 00 22
48 54 05 00 03 00 66 18 00 22
48 54 05 00 03 00 66 18 00 22
48 54 04 00 02 00 66 18 00 22
这样就方便使用了。

2、对单个文件进行处理

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#filename:exchange_newline2.py

import re,os



def newline(file_name):
    f = open(file_name,'r')
    content = f.readlines()
    f.close()

    f = open(file_name,'w')
    for line in content:
        f.writelines(re.sub('48','\n48',line))

    f.close()




while True:#对单个文件进行处理
    path = raw_input('INPUT THE FILEPATH(eg F:\sscom\crt.TXT):')
    file_path = r'%s' % path
    newline(file_path)
    print "FINISHED,OK"

输出:

>>> ================================ RESTART ================================
>>> 
INPUT THE FILEPATH(eg F:\sscom\crt.TXT):F:\tmp\1307\sscom\tmp2\mode_test\custom.TXT
FINISHED,OK
INPUT THE FILEPATH(eg F:\sscom\crt.TXT):

3、使用带命令行模式输入文件路径,处理单个文件

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#filename:exchange_newline1.py

import re,os
from sys import argv


def newline(file_name):
    f = open(file_name,'r')
    content = f.readlines()
    f.close()

    f = open(file_name,'w')
    for line in content:
        f.writelines(re.sub('48','\n48',line))

    f.close()


script, path = argv
file_path = r'%s' % path
newline(file_path)

输出:

D:\>cd Python\tmp

D:\Python\tmp>python exchange_newline1.py F:\tmp\1307\sscom\tmp2\mode_test\custo
m.TXT

D:\Python\tmp>


这样就基本都能满足需求了,不过文件只支持txt文件,office文件不支持。






posted on 2022-07-05 18:12  我在全球村  阅读(74)  评论(0编辑  收藏  举报