python将字典写入excel_在python中将字典字典写入csv

该博客讨论如何将包含嵌套列表的字典写入CSV文件。作者目前只能写出外层键值对,但目标是按特定格式输出,包括内层的时间和值。他们尝试只写外层键,但结果为空。解决方案建议使用Excel库如`xlsxwriter`,以实现所需格式的输出。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

I have a dictionary of dictionaries that I would like to write out to csv. my dictionary looks like:

dict={

object1:

{

time1:[value1,value2],

time2:[value3,value4]

},

object2:

{

time1:[value5,value6],time2[value7,value8]

}

}

I would like it to write out as:

object1

time1, value1, value2

time2, value3, value4

object2

time1, value5, value6

time2, value7, value8

Thus far, I can only figure out how to write the key/value pairs out csv using the following code:

writer = csv.writer(open('test.csv','wb'))

for key, value in Dict.items():

writer.writerow([key,value])

So it looks like:

objcet1,{

time1:[value1,value2],time2:[value3,value4]

}

objcet2,{

time1:[value5,value6],time2:[value7,value8]

}

I have tried writing just the outer keys (keys labeled object1 and object2) using

writer = csv.writer(open('test.csv','wb'))

for key, value in Dict.key():

writer.writerow([key])

but it just writes out a blank file. I think If I could figure out how to write the outer keys by themselves, I would be able to write out the csv as I want. Does anyone have any suggestions?

解决方案

I personally recommend using Excel, I have been working on .csv files, but ended up using .xlsx.

import xlsxwriter

workbook = xlsxwriter.Workbook('File.xlsx')

worksheet = workbook.add_worksheet()

row = 0

col = 0

for key in dictionary.keys():

row += 1

worksheet.write(row, col, key)

for item in dictionary[key]:

worksheet.write(row, col + 1, item)

worksheet.write(row, col + 2, dictionary[key][item][0])

row += 1

workbook.close()

And there you can change the columns and the rows increment so that you get the excel table you want to.

You can even export it as a .csv file afterwards from Excel.

Python中,你可以使用内置的csv模块来将大量的float数据写入一个CSV文件,每个数据写入一列。首先,你需要导入csv模块,然后创建一个文件对象,并使用csv.writer或csv.DictWriter来写入数据。以下是一个基本的示例代码: ```python import csv # 假设data是一个二维列表,每个内部列表代表一行数据,每个元素代表一列 data = [ [1.1, 2.2, 3.3], [4.4, 5.5, 6.6], # ... 可以添加更多的行 ] # 打开文件准备写入,'w'表示写入模式,newline=''确保跨平台写入时行尾统一 with open('output.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) # 写入数据,每一行代表data列表中的一行,每个元素代表一列 for row in data: writer.writerow(row) # 如果你想要将数据以字典形式写入,并且有表头,可以使用csv.DictWriter # fieldnames为表头,对应字典的键 fieldnames = ['Column1', 'Column2', 'Column3'] with open('output_with_header.csv', 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() # 写入表头 # 写入数据,每一行是一个字典 for row in data: writer.writerow({fieldnames[i]: value for i, value in enumerate(row)}) ``` 这段代码首先创建了一个名为`output.csv`的文件,并写入了一系列float值。每一个内部列表的元素代表一行中的一列。如果你想在CSV文件中包含表头,你可以使用`csv.DictWriter`并指定`fieldnames`参数。每个内部列表的元素会被写入到对应的列中。 请确保在写入数据前,路径是可访问的,并且你有足够的权限写入文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值