我有一个名为“玩家”的列表,其中包含词典。它看起来像这样:
players = [{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player1', 'bank': 0.06},
{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player2', 'bank': 4.0},
{'dailyWinners': 1, 'dailyFree': 2, 'user': 'Player3', 'bank': 3.1},
{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player4', 'bank': 0.32}]
它要长得多,但这是一段摘录。如何将这个词典列表输出到excel fils,以便按键/值整齐地组织?
非常感谢!
答案 0 :(得分:9)
有一种方法可以将字典列表写入excel工作表。首先请确保您拥有XlsxWriter package
。
from xlsxwriter import Workbook
players = [{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player1', 'bank': 0.06},
{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player2', 'bank': 4.0},
{'dailyWinners': 1, 'dailyFree': 2, 'user': 'Player3', 'bank': 3.1},
{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player4', 'bank': 0.32}]
ordered_list=["user","dailyWinners","dailyFree","bank"] #list object calls by index but dict object calls items randomly
wb=Workbook("New File.xlsx")
ws=wb.add_worksheet("New Sheet") #or leave it blank, default name is "Sheet 1"
first_row=0
for header in ordered_list:
col=ordered_list.index(header) # we are keeping order.
ws.write(first_row,col,header) # we have written first row which is the header of worksheet also.
row=1
for player in players:
for _key,_value in player.items():
col=ordered_list.index(_key)
ws.write(row,col,_value)
row+=1 #enter the next row
wb.close()
我尝试了代码,成功地工作了
答案 1 :(得分:2)
<强> test.py 强>
from csv import DictWriter
players = [{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player1', 'bank': 0.06},
{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player2', 'bank': 4.0},
{'dailyWinners': 1, 'dailyFree': 2, 'user': 'Player3', 'bank': 3.1},
{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player4', 'bank': 0.32}]
with open('spreadsheet.csv','w') as outfile:
writer = DictWriter(outfile, ('dailyWinners','dailyFreePlayed','dailyFree','user','bank'))
writer.writeheader()
writer.writerows(players)
运行python test.py
然后在Excel中打开生成的spreadsheet.csv
文件。
注意:我正在运行Linux,所以我无法使用Microsoft Excel进行测试。这适用于LibreOffice Calc,并提供一个电子表格,其中键是列名称,值在相应的列下。
答案 2 :(得分:1)
使用pandas的解决方案
import pandas as pd
players = [{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player1', 'bank': 0.06},
{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player2', 'bank': 4.0},
{'dailyWinners': 1, 'dailyFree': 2, 'user': 'Player3', 'bank': 3.1},
{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player4', 'bank': 0.32}]
df = pd.DataFrame.from_dict(players)
print (df)
df.to_excel('players.xlsx')
答案 3 :(得分:1)
xlsxwriter
库非常适合创建.xlsx
文件(最初由Fatih1923提及)。
以下代码段根据字典列表生成.xlsx
文件,同时说明顺序和显示的名称:
import xlsxwriter
# ...
def create_xlsx_file(file_path: str, headers: dict, items: list):
with xlsxwriter.Workbook(file_path) as workbook:
worksheet = workbook.add_worksheet()
worksheet.write_row(row=0, col=0, data=headers.values())
header_keys = list(headers.keys())
for index, item in enumerate(items):
row = map(lambda field_id: item.get(field_id, ''), header_keys)
worksheet.write_row(row=index + 1, col=0, data=row)
用法
headers = {
'bank': 'Money in Bank',
'dailyWinners': 'Daily Winners',
'dailyFree': 'Daily Free',
'user': 'User',
}
players = [
{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player1', 'bank': 0.06},
{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player2', 'bank': 4.0},
{'dailyWinners': 1, 'dailyFree': 2, 'user': 'Player3', 'bank': 3.1},
{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player4', 'bank': 0.32}
]
create_xlsx_file("my xslx file.xlsx", headers, players)
?注意-
headers
字典表示顺序和显示的名称。如果您未使用Python3.6 +,请使用OrderedDict
中的headers
,因为dict
中的顺序不会保留