gpt4 book ai didi

python - 索引 Python 打印语句

转载 作者:行者123 更新时间:2023-12-04 18:50:17 24 4
gpt4 key购买 nike

我的代码逐行获取日志文件输入,对其进行解析并创建格式化输出。

输入的每一行类似于"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

代码有什么问题?

最佳答案

这是更干净、更快的代码(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| |

关于python - 索引 Python 打印语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58506596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com