我要导出以下格式的字典:
handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value // Remove the [ and ] around value
});
}
到如下所示的列:
我曾尝试使用pandas和xlsxwriter,但我无法使其工作
答案 0 :(得分:3)
怎么样?
import pandas as pd
keys = my_dict.keys()
values = my_dict.values()
在熊猫中构建数据框,然后将其转换为“ csv”:
df = pd.DataFrame({"Start": keys, "Quantity": values})
df.to_csv("fname.csv")
或者,如果愿意,也可以直接作为'xlsx':
df.to_excel("fname.xlsx")
答案 1 :(得分:2)
您可以使用熊猫整齐地执行此操作,只需将路径更改为要保存Excel工作表的位置即可。但是,解析字典时不会保留key:value对的顺序,这可能对您来说是个问题。
import pandas as pd
mydict= {'66': 74, '62': 32, '69': 18, '72': 14, '64': 37, '192': 60, '51': 70, '46': 42, '129': 7, '85': 24, '83': 73, '65': 14, '87': 28, '185': 233, '171': 7, '176': 127, '89': 42, '80': 32, '5':
54, '93': 56, '104': 53, '138': 7, '162': 28, '204': 28, '79': 46, '178': 60, '144': 21, '90': 136, '193': 42, '88': 52, '212': 22, '199': 35, '198': 21, '149': 22, '84': 82, '213': 49, '47': 189, '195': 46, '31': 152, '71': 21, '70': 4, '207': 7, '158': 14, '109': 7, '163': 46, '142': 14, '94': 14, '173': 11, '78': 7, '134': 7, '96': 7, '128': 7, '54': 14, '63': 4, '120': 28, '121': 7, '37': 22, '13': 7, '45': 14, '23': 10, '180': 7, '50': 14, '188': 35, '24': 7, '139': 18, '148': 12, '151': 4, '2': 18, '34': 4, '77': 32, '81': 44, '82': 11, '92': 19, '95': 29, '98': 7, '217': 21, '172': 14, '35': 148, '146': 7, '91': 21, '103': 21, '184': 28, '165': 7, '108': 7, '112': 7, '118': 7, '159': 7, '183': 7, '186': 7, '205': 7, '60': 7, '67': 7, '76': 7, '86': 7, '209': 7, '174': 7, '194': 1}
df = pd.DataFrame()
df['Start'] = mydict.keys()
df['Quantity'] = mydict.values()
df.to_excel("C:\Users\David\Desktop\dict_test.xlsx")
答案 2 :(得分:1)
如果您使用的是 Python3.6 + ,则会对字典进行排序。您可以在Are dictionaries ordered in Python3.6+上了解有关详细信息的更多信息。
如果满足这些版本,则可以这样使用xlsxwriter
:
import xlsxwriter
d = {'66': 74, '62': 32, '69': 18}
# Create an new Excel file and add a worksheet.
with xlsxwriter.Workbook('demo.xlsx') as workbook:
# Add worksheet
worksheet = workbook.add_worksheet()
# Write headers
worksheet.write(0, 0, 'Start')
worksheet.write(0, 1, 'Quanitity')
# Write dict data
for i, (k, v) in enumerate(d.items(), start=1):
worksheet.write(i, 0, k)
worksheet.write(i, 1, v)
否则,以有序的顺序(例如元组列表)存储数据,然后执行相同的操作:
import xlsxwriter
d = [('66', 74), ('62', 32), ('69', 18)]
# Create an new Excel file and add a worksheet.
with xlsxwriter.Workbook('demo.xlsx') as workbook:
# Add worksheet
worksheet = workbook.add_worksheet()
# Write headers
worksheet.write(0, 0, 'Start')
worksheet.write(0, 1, 'Quanitity')
# Write list data
for i, (k, v) in enumerate(d, start=1):
worksheet.write(i, 0, k)
worksheet.write(i, 1, v)
如果您希望代码适用于所有python版本,则此方法更安全。
demo.xlsx:
答案 3 :(得分:1)
这就是我要怎么做
首先,您需要pandas
和openpyxl
,所以请先获取它们
df = pd.DataFrame.from_records(list(data.items()), columns=['Start', 'Quantity'])
writer = pd.ExcelWriter('out.xlsx')
df.to_excel(writer, 'Sheet1', index=False)
writer.save()
其中data
是您的字典
答案 4 :(得分:1)
希望这会有所帮助,您首先需要重新排列字典中包含的信息,并使用pandas Excel Writer将其保存
import pandas as pd
dic = {'66': 74, '62': 32, '69': 18, '72': 14, '64': 37, '192': 60, '51': 70, '46': 42, '129': 7, '85': 24, '83': 73, '65': 14, '87': 28, '185': 233, '171': 7, '176': 127, '89': 42, '80': 32, '5':
54, '93': 56, '104': 53, '138': 7, '162': 28, '204': 28, '79': 46, '178': 60, '144': 21, '90': 136, '193': 42, '88': 52, '212': 22, '199': 35, '198': 21, '149': 22, '84': 82, '213': 49, '47': 189, '195': 46, '31': 152, '71': 21, '70': 4, '207': 7, '158': 14, '109': 7, '163': 46, '142': 14, '94': 14, '173': 11, '78': 7, '134': 7, '96': 7, '128': 7, '54': 14, '63': 4, '120': 28, '121': 7, '37': 22, '13': 7, '45': 14, '23': 10, '180': 7, '50': 14, '188': 35, '24': 7, '139': 18, '148': 12, '151': 4, '2': 18, '34': 4, '77': 32, '81': 44, '82': 11, '92': 19, '95': 29, '98': 7, '217': 21, '172': 14, '35': 148, '146': 7, '91': 21, '103': 21, '184': 28, '165': 7, '108': 7, '112': 7, '118': 7, '159': 7, '183': 7, '186': 7, '205': 7, '60': 7, '67': 7, '76': 7, '86': 7, '209': 7, '174': 7, '194': 1}
table = pd.DataFrame(dic, index=[0])
y = [int(item) for item in table.columns.tolist()]
table.loc[1] = table.loc[0]
table.loc[0] = y
table = table.transpose()
table.columns = ['Start', 'Quantity']
table.index = list(range(len(table.index)))
writer = pd.ExcelWriter('output.xlsx')
table.to_excel(writer,'Sheet1', index = False)
writer.save()
答案 5 :(得分:-1)
我建议使用导入csv
import csv
from collections import OrderedDict
my_dict = OrderedDict() #pass in the ordered dict values or populate here
with open('names.csv', 'w', newline='') as csvfile:
fieldnames = ['Start', 'Quantity']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for k, v in my_dict.items():
writer.writerow({'Start': k, 'Quantity': v})
字典没有顺序,因此,如果您要以此方式进行操作,则需要创建一个有序的字典并按顺序添加值。这会将其另存为csv,也可以将其另存为.xls