【问题标题】:Python - Data not appending to list & Missing dict key after Read FilePython - 读取文件后数据未附加到列表和缺少字典键
【发布时间】:2021-11-30 01:43:54
【问题描述】:

我有一个名为“machinary.txt”的 txt 文件。这是一个示例数据,我只想抓取从“Section”开始的数据以及在它下面获得的相应输入、输出、位匹配、输入和输出。第一个“Section”之前的任何内容都将被忽略。

txt文件中的'BIT_MATCH',指的是它下面的'input->'和'output->'。 Section_A 有两个输入和输出。

任何“空”值都被定义为“N/A”。

'machinary.txt'

unwanted data
unwanted data
Input-> 000
Output-> 000
unwanted data
Codex153 @ Section_A_Machinary
Input-> 101
Output-> 010
unwanted data
unwanted data
BIT_MATCH: It matched at bit: 1
Input Obtained: 2
Output Obtained: 2
unwanted data
BIT_MATCH: It matched at bit: 2
Input Obtained: 3
Output Obtained: 3
unwanted data
unwanted data
Input-> 320
Output-> 321
unwanted data
unwanted data
Codex173 @ Section_B_Machinary
Codex183 @ Section_C_Distillery
BIT_MATCH: It matched at bit: 2
Input Obtained: 0
Output Obtained: 0
unwanted data
unwanted data
Input-> 011
Output-> 011

我已经完成的代码:

import pprint

with open("machinary.txt", "r") as file:
    flag = False
    headers = 'Section,Input,Output,Bit Matched'.split(',')

    sub_dict = dict.fromkeys(headers,'N/A')
    main_dict = {}
    bit_match_list = []
    input_list =[]
    output_list = []

    for eachline in file:

        if 'Section' in eachline:
            flag = True
            sub_dict['Section'] = eachline.strip().split()[-1]

        if flag:
            if 'BIT_MATCH' in eachline:
                bit_match_list.append(eachline.strip())
                bit_match_list.append(next(file).strip())
                bit_match_list.append(next(file).strip())
                sub_dict['Bit Matched'] = bit_match_list
                #sub_dict['Input bit match']=next(file).strip()
                #sub_dict['Output bit match'] = next(file).strip()

            if 'Input->' in eachline:
                input_list.append(eachline.strip())
                sub_dict['Input'] = input_list
                output_list.append (next(file).strip())
                sub_dict['Output'] = output_list
                main_dict[sub_dict['Section']] = sub_dict
                sub_dict = dict.fromkeys(headers, 'N/A')
                bit_match_list = []
                input_list = []
                output_list = []

pprint.pprint (main_dict)

以上代码的输出:

{'N/A': {'Bit Matched': ['BIT_MATCH: It matched at bit: 1',
                         'Input Obtained: 2',
                         'Output Obtained: 2',
                         'BIT_MATCH: It matched at bit: 2',
                         'Input Obtained: 3',
                         'Output Obtained: 3'],
         'Input': ['Input-> 320'],
         'Output': ['Output-> 321'],
         'Section': 'N/A'},
 'Section_A_Machinary': {'Bit Matched': 'N/A',
                         'Input': ['Input-> 101'],
                         'Output': ['Output-> 010'],
                         'Section': 'Section_A_Machinary'},
 'Section_C_Distillery': {'Bit Matched': ['BIT_MATCH: It matched at bit: 2',
                                          'Input Obtained: 0',
                                          'Output Obtained: 0'],
                          'Input': ['Input-> 011'],
                          'Output': ['Output-> 011'],
                          'Section': 'Section_C_Distillery'}}

预期输出:

{'Section_A_Machinary': {'Bit Matched': ['BIT_MATCH: It matched at bit: 1',
                         'Input Obtained: 2',
                         'Output Obtained: 2',
                         'BIT_MATCH: It matched at bit: 2',
                         'Input Obtained: 3',
                         'Output Obtained: 3'],
                         'Input': ['Input-> 101', 'Input-> 320'],
                         'Output': ['Output-> 010', 'Output->321'],
                         'Section': 'Section_A_Machinary'},
 'Section_B_Machinary': {'Bit Matched': 'N/A',
                         'Input': 'N/A',
                         'Output': 'N/A',
                         'Section': 'Section_B_Machinary'},
 'Section_C_Distillery': {'Bit Matched': ['BIT_MATCH: It matched at bit: 2',
                                          'Input Obtained: 0',
                                          'Output Obtained: 0'],
                          'Input': ['Input-> 011'],
                          'Output': ['Output-> 011'],
                          'Section': 'Section_C_Distillery'}}

很抱歉措辞冗长。不知何故,它错过了 Section_B。而 section_A 的 'input->' 和 'output->' 似乎没有像我想要的那样附加。 有没有简单的方法来解决这个问题,最好不要过多地改变上面的代码? 谢谢!

【问题讨论】:

  • 你没有正确重置你的sub_dict,这应该在部分中完成:新部分->新字典。您的默认值应该是预期的类型:输入/输出 -> list。如果你想要一些花哨的打印并显示 N/A 而不是一个空列表,然后构建你自己的打印功能,但这只会让你的逻辑膨胀

标签: python list dictionary


【解决方案1】:

要将文本解析为所需的结构,您可以使用re 模块:

txt = """
unwanted data
unwanted data
Input-> 000
Output-> 000
unwanted data
Codex153 @ Section_A_Machinary
Input-> 101
Output-> 010
unwanted data
unwanted data
BIT_MATCH: It matched at bit: 1
Input Obtained: 2
Output Obtained: 2
unwanted data
BIT_MATCH: It matched at bit: 2
Input Obtained: 3
Output Obtained: 3
unwanted data
unwanted data
Input-> 320
Output-> 321
unwanted data
unwanted data
Codex173 @ Section_B_Machinary
Codex183 @ Section_C_Distillery
BIT_MATCH: It matched at bit: 2
Input Obtained: 0
Output Obtained: 0
unwanted data
unwanted data
Input-> 011
Output-> 011
"""

import re
from itertools import chain

out = {}

for section in re.findall(r"Section(?:(?!Section).)+", txt, flags=re.S):
    bit_matches = re.findall(r"BIT_MATCH.*", section)
    inp_out = re.findall(
        r"Input Obtained.*?Output Obtained.*?$", section, flags=re.S | re.M
    )

    inputs = re.findall(r"Input->.*", section)
    outputs = re.findall(r"Output->.*", section)

    name = section.splitlines()[0]

    out[name] = {
        "Bit Matched": list(
            chain.from_iterable(
                (a, *b.splitlines()) for a, b in zip(bit_matches, inp_out)
            )
        )
        or "N/A",
        "Input": inputs or "N/A",
        "Output": outputs or "N/A",
        "Section": name,
    }

print(out)

打印:

{
    "Section_A_Machinary": {
        "Bit Matched": [
            "BIT_MATCH: It matched at bit: 1",
            "Input Obtained: 2",
            "Output Obtained: 2",
            "BIT_MATCH: It matched at bit: 2",
            "Input Obtained: 3",
            "Output Obtained: 3",
        ],
        "Input": ["Input-> 101", "Input-> 320"],
        "Output": ["Output-> 010", "Output-> 321"],
        "Section": "Section_A_Machinary",
    },
    "Section_B_Machinary": {
        "Bit Matched": "N/A",
        "Input": "N/A",
        "Output": "N/A",
        "Section": "Section_B_Machinary",
    },
    "Section_C_Distillery": {
        "Bit Matched": [
            "BIT_MATCH: It matched at bit: 2",
            "Input Obtained: 0",
            "Output Obtained: 0",
        ],
        "Input": ["Input-> 011"],
        "Output": ["Output-> 011"],
        "Section": "Section_C_Distillery",
    },
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2018-11-28
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多