根据ETL报错的信息,用Python从源文件中提取出错误的内容

# -*- coding:utf-8 -*-
#标准代码
import linecache
#lines存储错误的行
lines=[]        #错误行,字符串形式列表
error_lines=[]  #错误行,不包括首行
int_lines=[]    #错误行,整数形式列表,不包括首行
error_lines_file_path=r'C:\Users\yawei.chen\Desktop\Puck\Python\Job\test\error_seq.txt' #需要抽取的错误行数
source_file_path=r'C:\Users\yawei.chen\Desktop\Puck\Python\Job\test\iclog100.y2018.d0513.s00001.20180513002906.txt'#源文件
extract_file_path=r"C:\Users\yawei.chen\Desktop\IM22407681.txt"   #抽取的错误行文件,存储位置和文件名

#可控制函数
def truncate_file(error_lines_file_path,source_file_path,extract_file_path):
    # 读取错误的行数,并输出到error_lines
    with open(error_lines_file_path, "r", encoding='utf-8') as y:
        for line in y:
            lines.append(line)
            error_lines = lines[2:len(lines) + 1]

        # 生成int_lines,将error_lines中的字符串转换成int格式
        for int_line in error_lines:
            int_lines.append(int(int_line))

    # 在源文件中,提取错误行的内容
    with open(source_file_path, 'r', encoding='utf-8') as f1, \
            open(extract_file_path, 'w', encoding='utf-8') as f2:
        # 将错误行写入到extract_file_path文件中
        for int_line in int_lines:
            count = linecache.getline(source_file_path, int_line)
            f2.write(count)

truncate_file(error_lines_file_path,source_file_path,extract_file_path)

猜你喜欢

转载自blog.csdn.net/vaychen/article/details/80581185
今日推荐