简体   繁体   English

如何从包含键的字典和包含到相应键的数据的列表中将数据另存为csv文件

[英]How to save data as csv file from a dictionary containing keys and lists containing data to respective keys

I need some help on how to save data as a csv file consisting of keys with respective data in colon form. 我需要一些有关如何将数据另存为csv文件的帮助,该文件由具有各自冒号形式的键的键组成。 My code does this only if the dictionary contains one key and one list, but if i have more keys and more lists, the code manages to save the keys as i want them, that is to say, as rows but the data from all lists is saved in the first key. 我的代码仅在字典包含一个键和一个列表的情况下执行此操作,但是如果我有更多的键和更多的列表,则代码会设法将键保存为我想要的键,也就是说,保存为行,但是所有列表中的数据保存在第一个键中。 Any ideas are really appreciated 任何想法都非常感谢

I've tried googling most answers on csv file writing but I haven't found any that resolves my issue 我已经尝试过搜索有关csv文件编写的大多数答案,但没有找到能解决我的问题的任何答案

The code is as follows: 代码如下:

with open('C:/Users/..../Voltage.csv','wb') as csvFile:
                output = csv.writer(csvFile, delimiter=';', quoting = csv.QUOTE_ALL)
                output.writerow(voltdict.keys())
                for item,value in voltdict.iteritems():
                    for k in value:
                        output.writerow(localize_float([k])) 
            csvFile.close()

This an example of what i wish to accomplish: 这是我希望完成的一个示例:

key1 key2 keyn
123  232  253
245  398  ..n

Voltdict is my dictionary and it looks like this: Voltdict是我的字典,它看起来像这样:

{'key1': [list1] 'key2': [list2] 'key:n' : [listn]}

This is an example of what i get with more than one key and more than one list 这是我使用多个按键和多个列表的示例

key1 key2 key:n
122
245
232
398
253
..n

Just convert your dictionary to a pandas dataframe, then use to csv. 只需将字典转换为pandas数据框,然后将其用于csv。 See below 见下文

import pandas as pd
df=pd.DataFrame(my_dictionary)
df.to_csv(file_name)

When creating a DataFrame, simply pass a dictionary into the function, and the data should be structured properly. 创建DataFrame时,只需将字典传递到函数中即可,并且数据的结构应正确。 Here is an example 这是一个例子

list1=[2,3,4]
list2=[2,3,4]
listn=[1,2,3]
dictionary={'key1': list1, 'key2': list2, 'keyn' : listn}
df=pd.DataFrame(dictionary)

which outputs 哪个输出

   key1  key2  keyn
0     2     2      1
1     3     3      2
2     4     4      3

Looking at your code i see two problems. 查看您的代码,我看到两个问题。 First, you need commas between your keys when you create a dictionary. 首先,创建字典时,键之间需要用逗号隔开。 Second, if list1 , list2 and listn are already lists when they are defined, then you don't need additional brackets around them. 其次,如果list1list2listn在定义它们时已经是列表,那么您就不需要在它们周围附加括号。 Doing this would create a list of lists where the out list has only one entry. 这样做会创建一个列表列表,其中out列表只有一个条目。

If you have any other issues let me know 如果您还有其他问题,请通知我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何在Perl中读取此数据结构? 具有包含包含列表的列表的键的字典/哈希。 Python :: Inline给我错误 - How would I read this data structure in Perl? Dictionary/Hash with keys containing lists containing lists. Python::Inline giving me errors 如何以包含Django中键的列表的相同顺序在字典中添加数据? - How to add data in a dictionary in the same order of the list containing the keys in Django? 将 CSV 文件作为包含第一行作为标题(键)的字典读取 - Read CSV file as a dictionary containing first row as headers (keys) 如何从嵌套字典创建包含子字典键的列表? - How to create a List containing a sub dictionary keys , from nested dictionary? 如何将字符串转换为包含两个具有各自值的键的字典对象列表? - How to convert the string to a list of dictionary objects containing two keys with respective values? Python CSV文件到数据字典的多个键 - Python csv file to data dictionary multiple keys 包含键和列表列表的字典 - Dict containing keys with lists of lists 熊猫:如何展开包含字典的数据框行,其中字典中的列具有不同的键? - Pandas: How to expand data frame rows containing a dictionary with varying keys in a column? 如何获取包含字典的值和键的列表? - How to get a list containing the values and the keys of a dictionary? 如何将数据从 csv 文件保存到字典? - How to save data from csv file to dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM