Python字典到xlsx中的列

z9ju0rcb  于 2023-03-16  发布在  Python
关注(0)|答案(5)|浏览(151)

我希望导出以下格式的词典:

{'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}

如下所示:

我试过使用panda和xlsxwriter,但无法正常工作

vxqlmq5t

vxqlmq5t1#

如果你使用的是Python3.6+,那么字典是有序的。你可以在Are dictionary 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版本,这种方法会更安全。

  • 演示文件.xlsx:*

pbpqsu0x

pbpqsu0x2#

这个怎么样?

import pandas as pd

keys = my_dict.keys()
values = my_dict.values()

在Pandas中构建 Dataframe ,然后将其转换为'csv':

df = pd.DataFrame({"Start": keys, "Quantity": values})
df.to_csv("fname.csv")

或者,如果愿意,直接作为“xlsx”:

df.to_excel("fname.xlsx")
piztneat

piztneat3#

你可以使用Pandas来轻松地完成这件事,只需要将路径更改到你想要保存Excel工作表的位置,因为Python 3.6字典是有序的,但是要注意,在旧版本中,字典的顺序是不保留的。

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}
    
    #create empty data frame
    df = pd.DataFrame()
    
    #Use keys to create 'Start' column
    df['Start'] = mydict.keys()
    
    #Use values to create 'Quantity' column
    df['Quantity'] = mydict.values()
    
    #write to excel    
    df.to_excel("C:\Users\User1\Desktop\dict_test.xlsx")
omtl5h9j

omtl5h9j4#

希望这有帮助,你首先需要重新排列包含在字典中的信息,并保存它与PandasExcel 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()
nfzehxib

nfzehxib5#

我会这样做
首先,您需要pandasopenpyxl,因此请先获取它们

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是您的字典

相关问题