如何从txt文件的一行数据生成子词典?

2024-06-07 19:08:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.txt文件需要转换成口述: (行的顺序可能会有所不同)

name : john doe
age : 23
gender : MALE
address : kendall 6, Miami
career: mechanical engineer
times going : 2
number of assignments : 4
semester : 4
average : 9.2
interests : gaming, robotics, drama movies
availability:
    friday : 6:30 - 10:30
    sunday : 12:30 - 13:30
    monday : 16:30 - 18:30

输出代码应如下所示:


{'name': 'john doe',
 'age': '23',
 'gender': 'MALE',
 'address': 'kendall 6, Miami',
 'semester': '4',
 'career': 'mechanical engineer',
 'average': '9.2',
 'times going': '2',
 'number of assignments': '5',
 'interests': 'gaming, robotics, drama movies',
 'availability':
                {'friday': (630,1030),
                 'sunday': (1230,1330),
                 'monday': (1630,1830)
                 }
}

就目前而言,我已经成功地在“可用性”部分之前制作了词典:


dicc={}
listRestrictions=["availability","monday","tuesday","wednesday","thursday","friday","saturday","sunday"]

for line in file:
    line = line.strip("\n").replace(" : ", ":").strip(" ")
    key = line[: line.index(":")]
    if key not in listRestrictions:
        value = line[line.index(":") + 1 :]
        dicc[key] = value

print(dicc)

和打印:

{'name': 'john doe', 'age': '23', 'gender': 'MALE', 'address': 'kendall 6, Miami', 'career': 'mechanical engineer', 'times going': '2', 'number of assignments': '4', 'semester': '4', 'average': '9.2', 'interests': 'gaming, robotics, drama movies'}

(请记住它可能位于.txt文件的任何文件中,并且日期将始终位于“可用性”下) 我如何将“可用性”作为值,然后将日期作为子字典,如上所示


Tags: 文件nameageaddresslinejohngendermale
3条回答

不知道你为什么要尝试这么低层次的解析。那不是亚姆吗?这个假设让我非常接近:

import yaml
from pprint import pprint

with open('data.txt') as f:
    data = yaml.load(f)

那么data就是这个嵌套的Python字典:

{'name': 'john doe',
 'age': 23,
 'gender': 'MALE',
 'address': 'kendall 6, Miami',
 'career': 'mechanical engineer',
 'times going': 2,
 'number of assignments': 4,
 'semester': 4,
 'average': 9.2,
 'interests': 'gaming, robotics, drama movies',
 'availability': {'friday': '6:30 - 10:30',
                  'sunday': '12:30 - 13:30',
                  'monday': '16:30 - 18:30'}}

剩下的区别很容易做到,因为它是Python数据结构

    dicc={}

    last_key = ''

    for _line in file:

        line = _line.strip("\n").replace(" : ", ":")

        line = line.strip(" ")
        key = line[: line.index(":")]
        last_key = key
        value = line[line.index(":") + 1 :]
        if // the first part of the line is a blank space:
            dicc[last_key][key] = value
        else:
            dicc[key] = value

    print(dicc)
dicc = {}
for line in file:
        isAppended = line.startswith("    ")
        line = line.strip("\n").replace(" : ", ":").strip(" ")
        value = line[line.index(":") + 1 :]
        tempKey = line[: line.index(":")]
        if len(value)==0:
            currKey = tempKey
            tempDict = {}
        elif isAppended:
            tempDict[tempKey] = value
            dicc.update({currKey:tempDict})
        else:
            dicc[tempKey] = value

相关问题 更多 >

    热门问题