读取 txt 去除 txt 中异常出现的换行符

331 篇文章 3 订阅

all any # 两个自带的函数非常好用

@timethis
def read_txt_sap_fbl3n(txt_path, month_idx='2023/04/01', session=session_local):
    element_list = ["成本中心", "科目", "本國幣計之金額", 'BusA', '物料']
    # 去除中间不应该出现的换行符,生成新的 txt 文档
    with open(txt_path, 'r', encoding='utf-8') as f:
        line_list = []
        lines = f.readlines()
        print(len(lines))
        for line in lines:
            # print(line)
            if line.startswith('|') and line and line[-2] != '|' and line[-1] == '\n':
                line = line.rstrip('\n')
            line_list.append(line)
        with open(txt_path, 'w', encoding='utf-8') as f:
            for line in line_list:
                f.write(line)

    with open(txt_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        item_list = []
        print(len(lines))
        for line in lines:
            line = line.split('|')
            line = [i.strip() for i in line]
            if len(line) < 5:
                continue
            if all(element in line for element in element_list):
                code_center_index = line.index('成本中心')
                category_index = line.index('科目')
                plant_index = line.index('BusA')
                material_index = line.index('物料')
                amount_ntd_index = line.index('本國幣計之金額')
                print('line', line)
                print('code_center_index', code_center_index)
                continue

            item_dict = {}
            item_dict['month_idx'] = month_idx
            item_dict['code_center'] = line[code_center_index]
            item_dict['category'] = line[category_index]
            item_dict['plant'] = line[plant_index]
            item_dict['material'] = line[material_index]
            amount_ntd = line[amount_ntd_index]
            amount_ntd = amount_ntd.strip().replace(',', '')
            if '-' in amount_ntd:
                amount_ntd = amount_ntd.replace('-', '')
                amount_ntd = '-' + amount_ntd
            item_dict['amount_ntd'] = amount_ntd

            # print(i)

            plant = line[plant_index]
            if plant:
                item_list.append(item_dict)

            print(len(item_list))

        print('len item list', len(item_list))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值