Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中重新排列字段_Python_File_Csv_Dictionary - Fatal编程技术网

在Python中重新排列字段

在Python中重新排列字段,python,file,csv,dictionary,Python,File,Csv,Dictionary,我试图让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

我试图让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'}
我可以知道怎样才能做到这一点吗?
多谢各位

您可以使用
OrderedDict

import csv
import os
def read_employees(file):
  csv.register_dialect('stfDialect', skipinitialspace=True, strict=True)
  with open(file, r') as f
    staffFile = csv.DictReader(f, dialect = 'stfDialect')
  return list(staffFile)

staffList = read_employees("Staff.csv")
for e in staffList:
  print({k: e[k] for k in ('Department', 'Salary', 'Name')})
 from collections import OrderedDict

 staffList = []
 fields_order = ["Department", "Salary", "Name"]
 for data in staffFile:
     o_d = OrderedDict()
     for field in fields_order:
        o_d[field] = data["field"]
     staffList.append(o_d)
 return staffList

谢谢,但我使用的是Python3。Python3.6是Python3的一个版本。如果您可以使用3.6或更高版本,这就是您想要的解决方案。早期版本您使用的是更慢、更复杂的OrderedDict。谢谢您的帮助,但我得到了一个错误:列表索引必须是整数或切片,而不是str。嗯,这只是打印部分。现在我展示了整个代码(与原始代码相比有一些改进)。