将文本文件中的数据组织到词典中时出现问题

2024-06-16 08:48:28 发布

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

我是python的新手,一直在从事一个项目,在该项目中,我必须将.txt文件中的数据组织到dictionary(字典的键值应该是该人的全名,而其他数据是字典形式的值)中的dictionary必须包括——

  • item
  • location
  • date of purchase
  • remarks
  • salespeople

.txt文档如下所示:

Jimothy Donovan
Guitar
California
20.6.2018
The frame is broken
ENDREMARK
Hank Pym
Daniel Bennet
X
Isaac Newton
Hourglass
San Jose
12.8.2019
The hourglass is not accurate anymore
would like replacement
ENDREMARK
Daniel Bennet
Ethan Cole
X

代码如下所示:

def process_data(file: TextIO) -> Dict:
    CustomerDataDict = {}
    lines = file.readline().strip()
    x = 0
    for line in file:
        key = lines[x].strip()
        CustomerDataDict[key[0]] = lines[x+1].strip()
        CustomerDataDict[key[1]] = lines[x+2].strip()
        CustomerDateDict[key[2]] = lines[x+3].strip()

        start = lines.index('ENDREMARK\n')
        list_remark = lines[3:start]
        full_remark = ''
        for n in list_remark:
            full_remark += n
        CustomerDataDict[key[3]] = full_remark

        end = lines.index('X\n')
        salesppl_list = lines[start + 1:end]
        salespeople = []
        for name in salesppl_list:
            following.append(name.strip())
       CustomerDataDict[key[4]] = salespeople

        x = end + 1

    return CustomerDataDict

Dictionary的格式应为:

{username: 
    {'item':itemname, 
     'location':location, 
     'date-of-purchase': DATE, 
     'remarks':remarks, 
     'salespeople': {names of salespeople}
    }
}

感谢您的帮助


Tags: ofkeyinforlocationstartlistfile
1条回答
网友
1楼 · 发布于 2024-06-16 08:48:28

您可以在文本行上使用迭代器,并根据您的结构进行遍历:

text = """Jimothy Donovan
Guitar
California
20.6.2018
The frame is broken
ENDREMARK
Hank Pym
Daniel Bennet
X
Isaac Newton
Hourglass
San Jose
12.8.2019
The hourglass is not accurate anymore
would like replacement
ENDREMARK
Daniel Bennet
Ethan Cole
X"""

lines = iter(text.split("\n"))    # create iterator (get data from the file)

customerData = dict()
while True:
    username = next(lines,None)
    if not username: break
    customerData[username]        = userData    = dict()
    userData["item"]              = next(lines)
    userData["location"]          = next(lines)
    userData["date-of-purchase"]  = next(lines)
    userData["remarks"]           = remarks     = list()
    userData["salespeople"]       = salespeople = set()
    while True:
        remark = next(lines)
        if remark == "ENDREMARK": break
        remarks.append(remark)
    while True:
        salesperson = next(lines)
        if salesperson == "X": break
        salespeople.add(salesperson)

只要数据符合预期结构,这将生成所需的字典

print(customerData)
    
{'Jimothy Donovan':
     {'item': 'Guitar',
      'location': 'California',
      'date-of-purchase': '20.6.2018',
      'remarks': ['The frame is broken'],
      'salespeople': {'Daniel Bennet', 'Hank Pym'}},
 'Isaac Newton':
     {'item': 'Hourglass',
      'location': 'San Jose',
      'date-of-purchase': '12.8.2019',
      'remarks': ['The hourglass is not accurate anymore', 'would like replacement'],
      'salespeople': {'Ethan Cole', 'Daniel Bennet'}}
}

[编辑]不使用中断语句的备用版本(毕竟不是那么糟糕):

customerData = dict()
username = next(lines,None)
while username is not None:
    customerData[username]        = userData    = dict()
    userData["item"]              = next(lines)
    userData["location"]          = next(lines)
    userData["date-of-purchase"]  = next(lines)
    userData["remarks"]           = remarks     = list()
    userData["salespeople"]       = salespeople = set()
    remark = next(lines) 
    while remark != "ENDREMARK":
        remarks.append(remark)
        remark = next(lines)
    salesperson = next(lines)
    while salesperson != "X":
        salespeople.add(salesperson)
        salesperson = next(lines)
    username = next(lines,None)

相关问题 更多 >