我有" transactions.txt"像这样的文件:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
并且功能如下:
1:5:iznos:15.02.2017.:usser
2:2:iznos:17.02.2017.:usser2
3:3:iznos:3.04.2017.:usser2
4:3:iznos:3.04.2017.:useer
5:7:iznos:5.05.2017.:usser2
6:3:iznos:16.06.2017.:usser3
7:8:iznos:18.06.2017.:usser
如何在两个输入日期之间打印交易?
P.S稍后我必须输入两个日期和用户名,然后仅在某个用户名的两个日期之间打印交易。所以,如果有人也可以帮助我......:)
答案 0 :(得分:0)
from csv import reader
from io import StringIO
from datetime import datetime
text = '''1:5:iznos:15.02.2017.:usser
2:2:iznos:17.02.2017.:usser2
3:3:iznos:3.04.2017.:usser2
4:3:iznos:3.04.2017.:useer
5:7:iznos:5.05.2017.:usser2
6:3:iznos:16.06.2017.:usser3
7:8:iznos:18.06.2017.:usser'''
from_date = datetime(2017, 4, 3)
to_date = datetime(2017, 5, 5)
# with open('data.txt', 'r') as file
with StringIO(text) as file:
data = reader(file, delimiter=':')
for line in data:
date = datetime.strptime(line[3], '%d.%m.%Y.')
if from_date <= date <= to_date:
print(line)
答案 1 :(得分:0)
首先,您需要读取数据;第二,你需要从每一行提取日期;第三,你需要检查日期是否在想要的范围内;所以解决方案是:
with open('transactions.txt') as f:
for line in f:
l_date = date.strptime(line.split(':')[3], "%d.%m.%Y.")
if date1 < l_date < date2:
print line