如何使用 Python 检索制表符格式文件中的值?

How to retrieve a value in a file with tab format using Python?

我有一个格式如下的文件:

FileConfig
;
;     Version: 4.0.0
;     Date="2021/11/19"
;
Name
    James
Number
    1091125
Identifier
    STDNT
Byte
        7687aBap
ID
    22SSWA
Program
    *Internal
    External

我想获取 NameNumber 等字符串的值。 我试过这种方式,但我只能得到所有的字符串。有没有办法通过初始化 'Name' 或 Number.

来获取特定值
file1 = open("myfile.log", "r")
print(file1.readlines())

我期望我可以通过这种方式获得价值:

file1.readlines(Name)

非常感谢您的帮助。我是 Python 的新手。非常感谢

我尝试将与您有关的信息保存到字典中,此逻辑基于您共享的文件:

count = 0
key = 0
dic = dict()
key_counter = 1
keys = ["Name","Number"]
with open("file.log") as fp:
    Lines = fp.readlines()
    for line in Lines:
        count += 1
        if "FileConfig" in line.strip() or ";" in line.strip():
            pass
        elif key_counter == 1 and line[0] != " ":
            key_counter = 2
            key= line.strip()
            if key in keys:
                dic[line.strip()] =0
        elif key_counter == 1 and line[0] == " ":
            key_counter = 1
            if key in keys:
                val = dic.get(key)
                if isinstance(val, list):
                    val.append(line.strip())
                    dic[key] =val
                else:
                    dic[key] = [val,line.strip()]
        elif key_counter == 2:
            if key in keys:
                dic[key] = line.strip()
            key_counter = 1
dic

输出:

  {'Name': 'James', 'Number': '1091125'}