图A - .txt文件中提供的数据
图B - 我试图解决它。到目前为止我能够完成的是检查.txt并将适当的记录存储到不同的列表中。我的目标是将每个记录与运行/完成的时间配对并将其传输到Access数据库。我正在考虑采用多维列表方法但完全不知道从哪里开始。
program1 STARTED AT 2017-06-07-14.31.34.827086
REC READ = 265
REC WRITTEN = 265
CALLS = 82
SHPR FOUND = 69
SHPR NOT FND = 3
ADDR FOUND = 3169
ADDR NOT FND = 0
INELIG SYS = 7
ELIG SYS = 3762
program1 COMPLETED AT 2017-06-07-14.35.56.067879
=========================================================
program1 STARTED AT 2017-06-07-14.31.34.827086
REC READ = 165
REC WRITTEN = 235
CALLS = 32
SHPR FOUND = 649
SHPR NOT FND = 23
ADDR FOUND = 31369
ADDR NOT FND = 10
INELIG SYS = 471
ELIG SYS = 762
program1 COMPLETED AT 2017-06-07-14.35.56.067879
我目前的代码如下(图B)
file = open(r"C:\Users\Desktop\joblogs.txt", "r")
rec_read = []
rec_written = []
calls = []
shpr_found = []
shpr_nf = []
addr_found = []
addr_nf = []
inelig_sys = []
elig_sys = []
for line in file:
line = line.strip()
if "REC READ" in line:
line = (line.split()[-1])
rec_read.append(line)
if "REC WRITTEN" in line:
line = (line.split()[-1])
rec_written.append(line)
if "CC01 CALLS" in line:
line = (line.split()[-1])
calls.append(line)
if "SHPR FOUND" in line:
line = (line.split()[-1])
shpr_found.append(line)
if "SHPR NOT FND" in line:
line = (line.split()[-1])
shpr_nf.append(line)
if "ADDR FOUND" in line:
line = (line.split()[-1])
addr_found.append(line)
if "ADDR NOT FND" in line:
line = (line.split()[-1])
addr_nf.append(line)
if "INELIG SYS" in line:
line = (line.split()[-1])
inelig_sys.append(line)
if "ELIG SYS" in line:
line = (line.split()[-1])
elig_sys.append(line)
print(rec_read)
print(rec_written)
print(calls)
print(shpr_found)
print(shpr_nf)
print(addr_found)
print(addr_nf)
print(inelig_sys)
print(elig_sys)
答案 0 :(得分:0)
这似乎适用于字典:
d = {}
count = 0
for line in file:
if '==' in line:
continue
if 'STARTED' in line:
start_time = line.split(' ')[-1]
d[count] = {'start_time': start_time}
continue
if 'COMPLETED' in line:
end_time = line.split(' ')[-1]
d[count].update({'end_time': end_time})
count += 1
continue
x, y = line.split('=')
x = x.strip()
y = y.strip()
d[count].update({x:y})
结果:
{0: {'ADDR FOUND': '3169',
'ADDR NOT FND': '0',
'CALLS': '82',
'ELIG SYS': '3762',
'INELIG SYS': '7',
'REC READ': '265',
'REC WRITTEN': '265',
'SHPR FOUND': '69',
'SHPR NOT FND': '3',
'end_time': '2017-06-07-14.35.56.067879',
'start_time': '2017-06-07-14.31.34.827086'},
1: {'ADDR FOUND': '31369',
'ADDR NOT FND': '10',
'CALLS': '32',
'ELIG SYS': '762',
'INELIG SYS': '471',
'REC READ': '165',
'REC WRITTEN': '235',
'SHPR FOUND': '649',
'SHPR NOT FND': '23',
'end_time': '2017-06-07-14.35.56.067879',
'start_time': '2017-06-07-14.31.34.827086'}}