我有一些代码,逐行接受日志文件输入,对其进行解析,然后创建格式化的输出。
输入的每一行都类似于"12|15|joe|2|N|15| | |"
。
输出应为...
1 12 OK--- 15|joe|2|N|15| |
2 15 NO--- 12|2|N|15|3|
3 12 OK--- 15|joe|2|N|15| |
但是我的代码正在打印...
line 12OK--- 15
line 12OK--- 15
line 15NO--- 12
line 15NO--- 12
这是我的代码:
import sys
import os
import re
print ("hello");
filename= "data.txt"
mydata= []
with open(filename, "r") as f:
next(f)
index = 1
logic = True
for i in f:
st = i.split('|')
if len(st) == 1:
break
record_number= st[0]
size=st[1]
username=st[2]
transaction=st[3]
field_type=st[4]
numeric_value=st[5]
character_value=st[6]
date_value=st[7]
if field_type == 'N':
if character_value != ' ' or date_value != ' ' or numeric_value == ' ':
logic=False
elif field_type == 'C':
if numeric_value != ' ' or date_value != ' ' or character_value == ' ':
logic=False
elif field_type == 'D':
if numeric_value != ' ' or character_value != ' ' or date_value == ' ':
logic=False
new_st = st[0] + '|' + st[1] + '|' + st[2] + '|' + st[3] +' |' + st[4] + '|' + st[5] + '|' + st[6] + '|' + st[7]+'|'
if logic:
print("line " + st[0] + "OK--- " + st[1])
else:
print ("line " + st[1] + "NO--- " + st[0])
index = index + 1
代码有什么问题?
答案 0 :(得分:0)
这更简洁,代码更快(str + str + str
慢:))
我希望它涵盖了您想要做的所有事情...
line = 0
for i in f:
line += 1
#=== this is probably not needed. File will stop producing data when it reaches the last line
# if len(i) < 1:
# break
#===========================================================================
record_number, size, username, transaction, field_type, numeric_value, character_value, date_value = i.split('|')
# Default, assuming nothing comes of the if/then cascade
line_to_print = "{} {} OK--- {}|{}|{}|{}|{}|{}|{}".format(line, record_number, size, username, transaction, field_type, numeric_value, character_value, date_value)
if field_type == 'N':
if character_value != ' ' or date_value != ' ' or numeric_value == ' ':
line_to_print = "{} {} NO--- {}|{}|{}|{}|{}|{}|{}".format(line, size, record_number, transaction, field_type, numeric_value, character_value, date_value)
elif field_type == 'C':
if numeric_value != ' ' or date_value != ' ' or character_value == ' ':
line_to_print = "{} {} NO--- {}|{}|{}|{}|{}|{}|{}".format(line, size, record_number, transaction, field_type, numeric_value, character_value, date_value)
elif field_type == 'D':
if numeric_value != ' ' or character_value != ' ' or date_value == ' ':
line_to_print = "{} {} NO--- {}|{}|{}|{}|{}|{}|{}".format(line, size, record_number, transaction, field_type, numeric_value, character_value, date_value)
print(line_to_print)
甚至还有一些pythonic:
def makeLineToPrint(ok):
if ok:
line_to_print = "{} {} OK--- {}|{}|{}|{}|{}|{}|{}".format(line, record_number, size, username, transaction, field_type, numeric_value, character_value, date_value)
else:
line_to_print = "{} {} NO--- {}|{}|{}|{}|{}|{}".format(line, size, record_number, transaction, field_type, numeric_value, character_value, date_value)
return line_to_print
line = 0
for i in f:
line += 1
#=== this is probably not needed. File will stop producing data when it reaches the last line
# if len(i) < 1:
# break
#===========================================================================
record_number, size, username, transaction, field_type, numeric_value, character_value, date_value = i.split('|')
# Default, assuming nothing comes of the if/then cascade
line_to_print = makeLineToPrint(ok = True)
if field_type == 'N':
if character_value != ' ' or date_value != ' ' or numeric_value == ' ':
line_to_print = makeLineToPrint(ok = False)
elif field_type == 'C':
if numeric_value != ' ' or date_value != ' ' or character_value == ' ':
line_to_print = makeLineToPrint(ok = False)
elif field_type == 'D':
if numeric_value != ' ' or character_value != ' ' or date_value == ' ':
line_to_print = makeLineToPrint(ok = False)
print(line_to_print)
输出:
1 12 OK--- 15|joe|2|N|15| |
2 15 NO--- 12|2|N|15|3|
3 12 OK--- 15|joe|2|N|15| |