我的报告按用户分为几个部分,然后再按日期划分。
报告如下:
ACCOUNT: 1
DATE BALANCE
04/04/18 11,421,231.94
04/05/18 16,545,275.32
ACCOUNT: 2
DATE BALANCE
04/04/18 4,822,236.39
04/05/18 4,445,253.14-
我需要在今天的余额和日期
旁边打印用户的帐号foreach Account in Accounts:
with open ('C:\intersum.recap.txt') as f:
for line in f:
if Account in line:
if "04/05/18" in line:
print(line) + Account
如何编写我的代码,以便每次在帐户中找到帐号时,它会在今天的余额中打印该帐户值?
理想情况下,输出为:
帐户1 04/05/18 4,445,253.14 -
答案 0 :(得分:0)
使用正则表达式。
import re
import datetime
toDayDate = datetime.datetime.now().strftime("%d/%m/%y")
with open ('C:\intersum.recap.txt') as f:
s = f.read() #Read file and get content.
account = re.findall("(?:ACCOUNT:).*", s)
balance = re.findall("(?:{0}).*".format(toDayDate), s)
for i in zip(account, balance):
print(i)
<强>输出:强>
('ACCOUNT: 1', '10/04/18 11,421,231.94')
('ACCOUNT: 2', '10/04/18 4,822,236.39')
答案 1 :(得分:0)
我最终使用以下内容解决了我的问题:
Accounts = ['1', '2', '3', '4', '5']
for Account in Accounts:
with open ('C:\intersum.recap.txt') as f:
for line in f:
if "ACCOUNT: " + Account in line:
#print(line)
TAccount = Account
for line in f:
if "04/05/18" in line:
#print(line)
Transaction = line
print(TAccount + " " + line)
break