【发布时间】:2020-07-26 03:03:22
【问题描述】:
我正在尝试让我的 python 程序在列表中显示员工、薪水和部门的名称。这是我的 csv 文件:
Name, Salary, Department
Pika Chu, 5000, Pokemon
Peter Parker, 10000, Friendly Neighborhood Spiderman
Bruce Lee, 25000, Kung Fu Master
Trevor Phillips, 30000, Grand Theft Auto
Willy Wonka, 1000000, Chocolatier
Mario, 84000, Plumber
Sonic, 20000, Runner
这是我的代码:
#!/usr/bin/env python3
import csv
import os
def read_employees(file):
open(file, 'r')
csv.register_dialect('stfDialect', skipinitialspace=True, strict=True)
staffFile = csv.DictReader(open(file), dialect = 'stfDialect')
staffList = []
for data in staffFile:
staffList.append(data)
return staffList
staffList = read_employees("Staff.csv")
print(staffList)
输出是这样的:
{'Name': 'Pika Chu', 'Salary': '5000', 'Department': 'Pokemon'}
但我希望输出显示这样的数据:
{'Department': 'Pokemon', 'Salary': '5000', 'Name': 'Pika Chu'}
我可以知道我怎样才能做到这一点吗? 非常感谢!
【问题讨论】:
标签: python file csv dictionary