繁体   English   中英

索引 Python 打印声明

[英]Index Python Print Statement

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

输入的每一行类似于"12|15|joe|2|N|15| | |" .

output 应该是...

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)

OUTPUT:

1 12 OK--- 15|joe|2|N|15| | 
2 15 NO--- 12|2|N|15|3| 
3 12 OK--- 15|joe|2|N|15| | 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM