Python 3.3 - 将字典粘贴到 Excel

标签 python dictionary paste xlwt

我想粘贴以下字典:

{'Olive Oil': 221.0, 'Ham - Pork': 216.14999999999998, 'Feta Cheese': 163.125, 'Vinegar': 5.1, 'Cherry Tomatoes': 22.5, 'Cucumber': 22.5}

在新的 Excel 工作簿中:

newbook = xlwt.Workbook(encoding="utf-8")
sheet = newbook.add_sheet('Table')

使用 xlwt

最佳答案

我用过类似的东西。希望这有帮助

import xlsxwriter

excelBook = xlsxwriter.Workbook('data.xlsx')
excelSheet = excelBook.add_worksheet()

elements = {'a':['e1','e2','e3'],
            'b':['e1','e2']
           } 
row = 0
col = 0

for key in elements.keys():
    row += 1
    excelSheet.write(row, col, key)
    for item in elements[key]:
        excelSheet.write(row, col + 1, item)
        row += 1

excelBook.close()

您还可以使用 DictWriter(在 csv 中)。

关于Python 3.3 - 将字典粘贴到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677798/

相关文章:

python - 使用 Python mysql.connector 连接到 MariaDB 时出现问题

TinyMCE:paste_enable_default_filters 保留颜色、img 和字体大小

.net - 继承 Dictionary(of TKey, TValue) 不能不区分大小写

linux - Linux中断中的剪切和粘贴

Java剪贴板调用粘贴操作

Python 从文本文件中提取不同长度的值

python - 像 Qlik 一样计算 pandas 数据框中的列中的唯一值?

python - 错误:index out of range

python - 在 python 中使用字典键作为不同字典中的值

c++ - 使用迭代器在 vector 中插入下一个值以进行映射