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))