【问题标题】:I need help pulling out specific data from a list of dictionaries我需要帮助从字典列表中提取特定数据
【发布时间】:2013-03-15 20:17:05
【问题描述】:

我有一个文本文件,其中列出了一组员工的姓名、身份证号、性别和工作年限。第一行包含员工人数。它被称为employees.txt。这里是:

7

约翰

母狗

33

272838

M

玛丽

约翰逊

38

3849383

F

奥佩

狗

6

839293

M

小姐

狗

4

238392

F

约翰

布拉

28

834282

M

阿米莉亚

斯宾

2

5789812

F

肖恩

卡森斯蒂恩

2

567891

M


我想将这些信息组织成一个字典列表,然后找到所有女性员工并打印出她们的名字和姓氏。我可以让字典列表工作,但是当我尝试查找并打印出女性员工的姓名时出现错误。这是我的代码:

def employee_write(file):
    employee_dict = {}
    employee_dict["First"] = file.readline().strip()
    employee_dict["Last"] = file.readline().strip()
    employee_dict["Experience"] = file.readline().strip()
    employee_dict["ID"] = file.readline().strip()
    employee_dict["Gender"] = file.readline().strip()
    return employee_dict

def female_print(x2):
    for term in x2:
        for word in employee_dict:
            if employee_dict["Gender"] == "F":
                print(employee_dict["First"])
                print(employee_dict["Last"])

def main():
    file = open("employees.txt", "r")
    n = int(file.readline())
    x1 = employee_write(file)
    employee_list = []
    for i in range(n):
        employee_list.append(x1)
        x2 = employee_list
    print(x2)
    female_print(x2)

main()

我的错误是什么? female_print() 函数不起作用,因为它表示未定义名称“employee_dict”。有什么问题?

【问题讨论】:

  • 请将您看到的错误添加到问题中。
  • 表示'function female_print(x2)'中的名字'employee_dict'没有定义。
  • 那是因为您没有 1) 将其传入,或 2) 将其声明为全局
  • 那么我该如何解决这个问题,Moshie?

标签: python list dictionary text-files


【解决方案1】:

我在理解你的变量命名和使用方面有些困难,所以我稍微简化了一下:

def employee_write(file):
    employee_dict = {}
    employee_dict["First"] = file.readline().strip()
    employee_dict["Last"] = file.readline().strip()
    employee_dict["Experience"] = file.readline().strip()
    employee_dict["ID"] = file.readline().strip()
    employee_dict["Gender"] = file.readline().strip()
    return employee_dict

def female_print(employee_list):
    for employee in employee_list:
        if employee["Gender"] == "F":
            print(employee["First"])
            print(employee["Last"])

def main():
    file = open("employees.txt", "r")
    n = int(file.readline())
    employee_list = []
    for i in range(n):
        employee_list.append(employee_write(file))
    print(employee_list)
    female_print(employee_list)

main()

产生

[{'Gender': 'M', 'Last': 'Doe', 'ID': '272838', 'Experience': '33', 'First': 'John'}, {'Gender': 'F', 'Last': 'Johnson', 'ID': '3849383', 'Experience': '38', 'First': 'Mary'}, {'Gender': 'M', 'Last': 'Dog', 'ID': '839293', 'Experience': '6', 'First': 'Opie'}, {'Gender': 'F', 'Last': 'Dog', 'ID': '238392', 'Experience': '4', 'First': 'Missy'}, {'Gender': 'M', 'Last': 'Bla', 'ID': '834282', 'Experience': '28', 'First': 'John'}, {'Gender': 'F', 'Last': 'Spien', 'ID': '5789812', 'Experience': '2', 'First': 'Amelia'}, {'Gender': 'M', 'Last': 'CasenStien', 'ID': '567891', 'Experience': '2', 'First': 'Shane'}]
Mary
Johnson
Missy
Dog
Amelia
Spin

这是你想要的吗?

【讨论】:

    【解决方案2】:

    这里有两个(主要)问题。一方面,您实际上并没有阅读整个文件。您正在读取第一条记录(作为 x1),然后将该记录附加到列表 N 次。您可以从打印中看到您只获得了一条记录。

    另外,您的 print_female 函数引用了一个不存在的变量“employee_dict”——您可能需要在某个地方定义它。也许您的意思是使用“术语”?这会引发堆栈跟踪。

    如果你解决了这两个问题,你的代码的基本前提是健全的。

    【讨论】:

    • 谢谢!我已经解决了这个问题。
    【解决方案3】:

    在main 中,函数employee_write(应该改为employee_read)只被调用一次。

    试试这样的:

    In [1]: with open('employees.txt', 'r') as ef:
        txt = ef.read()
       ...:     
    
    In [2]: lines = txt.split()
    
    In [3]: records = [lines[i:i+5] for i in xrange(1, len(lines), 5)]
    
    In [4]: records
    Out[4]: [['John', 'Doe', '33', '272838', 'M'], ['Mary', 'Johnson', '38', '3849383', 'F'], ['Opie', 'Dog', '6', '839293', 'M'], ['Missy', 'Dog', '4', '238392', 'F'], ['John', 'Bla', '28', '834282', 'M'], ['Amelia', 'Spien', '2', '5789812', 'F'], ['Shane', 'CasenStien', '2', '567891', 'M']]
    
    In []: for r in records:
        print r
       ....:     
    ['John', 'Doe', '33', '272838', 'M']
    ['Mary', 'Johnson', '38', '3849383', 'F']
    ['Opie', 'Dog', '6', '839293', 'M']
    ['Missy', 'Dog', '4', '238392', 'F']
    ['John', 'Bla', '28', '834282', 'M']
    ['Amelia', 'Spien', '2', '5789812', 'F']
    ['Shane', 'CasenStien', '2', '567891', 'M']
    

    注意:此代码假定输入文件的格式正确!

    【讨论】:

    • 请注意,数据文件的开头有一个标题行,其中包含记录数。
    • 糟糕。修复因记录数而导致的错误。感谢帝斯曼!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多