python - 仅保留 MT940 文件中的最新标签
问题描述
我正在使用 MT940 银行对账单文件加载到本地应用程序。每次将数据加载到此应用程序时,先前加载的数据将被丢弃并加载新的文件内容。
这样做的问题是,可能存在这样一种情况,即在第 2 天生成的文件与第 1 天相比可能具有较少数量的银行账户,因此应用程序将不包含第 1 天的数据。
所以我想合并第 1 天和第 2 天的 MT940 文件;并且只保留那些声明日期是最新的使用 python 脚本的声明。
我能够成功完成文件合并部分(代码如下所示),但我不知道如何从“只保留最新信息”的后半部分开始。
向大师伸出援手 - 非常感谢您提供任何帮助。谢谢!
# importing the library
import os
# giving directory name
dirname = 'C:/Downloads'
# giving file extension
ext = ('.txt')
with open('C:/Downloads/logs/file3.txt', 'w') as outfile:
# iterating over all files
for files in os.listdir(dirname):
if files.endswith(ext):
# Open each file in read mode
with open(dirname+'/'+files) as infile:
# read the data from file1 and
# file2 and write it in file3
outfile.write(infile.read())
# Add '\n' to enter data of file2
# from next line
outfile.write("\n")