python-删除txt文件空格并替换指定内容

1. 处理空行 删掉;
2. 找出顶行是空格,统一更换成 空格+空格+O
3. 遍历每行数据,如果这行的第三个字符是O,下行的第三个字符是I,那么就把这行的O改成I
4. 验证生成的txt,如果有问题输出到控制台

import os

"""
1. 处理空行 删掉;
2. 找出顶行是空格,统一更换成 空格+空格+O
3. 遍历每行数据,如果这行的第三个字符是O,下行的第三个字符是I,那么就把这行的O改成I
4. 验证生成的txt,如果有问题输出到控制台
"""

# 待读取文件夹地址,存放多个待读取的txt文件
read_path = "D:\标注\\test\\"
# 存放生成的txt文件的文件夹地址
write_path = "D:\标注\\testxire\\"

flag = False

for path in os.listdir(read_path):
    f = open(read_path + path, "r", encoding='utf-8')
    with open(write_path + path, "w", encoding='utf-8') as fn:
        lines = f.readlines()
        size = lines.__len__()
        for i in range(size):
            # 空行不写入
            if lines[i] != "\n":
                # 顶行是空格
                if lines[i].startswith(' '):
                    # 下行是I
                    if i != size - 1 and lines[i + 1][2] == 'I':
                        fn.write('  I\n')
                    else:
                        fn.write('  O\n')
                elif lines[i][2] == 'O' and lines[i + 1][2] == 'I':
                    fn.write('  I\n')
                else:
                    fn.write(lines[i])

#校验是否有OI的情况出现
for path in os.listdir(write_path):
    f = open(write_path + path, "r", encoding='utf-8')
    lines = f.readlines()
    size = lines.__len__()
    for i in range(size):
        if i != size - 1:
            if lines[i][2] == 'O' and lines[i + 1][2] == 'I':
                i = str(i+1)
                print(path+" 文件,第"+i+"行有问题,注意查看!")

 

你可能感兴趣的:(python)