繁体   English   中英

从字典键和值打印到文本文件

[英]printing into a text file from dictionary key and values

我需要一些代码的帮助,它需要将 go 写入日志文件,它应该如下所示:

日志文件示例

我有一个包含计数值的字典和作为事件 ID 的键,但我想这样显示它,但我不知道如何显示,因为它一次全部出现,它不会单独打印而不是 1 by 1,我已经使用嵌套字典来做到这一点。

这是包含需要打印的计数值和键的字典的示例。

eventIDs = {1102: {'count': 0}, 4611: {'count': 0}, 4624: {'count': 0}, 4634: {'count': 0}, 4648: {'count': 0}, 4661: {'count': 0},
        4662: {'count': 0}, 4663: {'count': 0}, 4672: {'count': 0}, 4673: {'count': 0}, 4688: {'count': 0}, 4698: {'count': 0},
        4699: {'count': 0}, 4702: {'count': 0}, 4703: {'count': 0}, 4719: {'count': 0}, 4732: {'count': 0}, 4738: {'count': 0},
        4742: {'count': 0}, 4776: {'count': 0}, 4798: {'count': 0}, 4799: {'count': 0}, 4985: {'count': 0}, 5136: {'count': 0},
        5140: {'count': 0}, 5142: {'count': 0}, 5156: {'count': 0}, 5158: {'count': 0}}

这是我尝试过的代码:

def log_output():
      with open('path' + timeStamp + '.txt', 'a') as VisualiseLog: 
        event_id = list{eventIDs.keys()}
        event_count = list(eventIDs.values)
        for item in eventIDs:
            print(f'Event ID: {event_id}')
            VisualiseLog.write('Event ID: {event_id}')
            print(f'Event Count: {event_count}')
            VisualiseLog.write(f'Event Count: {event_count}')

试试这个代码:


eventIDs = {
    1102: {'count': 0},
    4611: {'count': 0}
}

timeStamp = "1234"

def log_output():
    with open('path' + timeStamp + '.txt', 'a') as VisualiseLog:
        for id in eventIDs:
            count = eventIDs[id]['count']
            print(f'Event ID: {id}')
            VisualiseLog.write(f'Event ID: {id}\n')
            print(f'Event Count: {count}')
            VisualiseLog.write(f'Event Count: {count}\n\n')


log_output()



# Outputs:
#  Event ID: 1102
#  Event Count: 0
#
#  Event ID: 4611
#  Event Count: 0

暂无
暂无

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

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