Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将字典打印到csv,每个键值都在一个新行中_Python_Python 2.7_Pandas - Fatal编程技术网

Python:将字典打印到csv,每个键值都在一个新行中

Python:将字典打印到csv,每个键值都在一个新行中,python,python-2.7,pandas,Python,Python 2.7,Pandas,我有以下数据 {'index': [1, 2, 3], 'similar': [[0, 2], [1, 2], [2, 1]], 'markets': [['A', 'C'], ['B', 'C'], ['A', 'B']]} 我想将其打印到csv文件,如下所示: index similar markets 1 [0,2] ['A','C'] 2 [1,2] ['B','C'] 3 [2,1] ['A','B'] with open('m

我有以下数据

{'index': [1, 2, 3], 'similar': [[0, 2], [1, 2], [2, 1]], 'markets': [['A', 'C'], ['B', 'C'], ['A', 'B']]}
我想将其打印到csv文件,如下所示:

index   similar  markets
1       [0,2]   ['A','C']
2       [1,2]   ['B','C']
3       [2,1]   ['A','B']
with open('mycsvfile.csv', 'wb') as f:  # Just use 'w' mode in 3.x
    w = csv.DictWriter(f, a.keys())
    w.writeheader()
    w.writerow(a)
目前我的代码如下:

index   similar  markets
1       [0,2]   ['A','C']
2       [1,2]   ['B','C']
3       [2,1]   ['A','B']
with open('mycsvfile.csv', 'wb') as f:  # Just use 'w' mode in 3.x
    w = csv.DictWriter(f, a.keys())
    w.writeheader()
    w.writerow(a)
它会打印:

 index               similair                       markets
[1, 2, 3]   [[0, 2], [1, 2], [2, 1]]    [['A', 'C'], ['B', 'C'], ['A', 'B']]
CSV文件:

index,similar,markets
1,"[0, 2]","['A', 'C']"
2,"[1, 2]","['B', 'C']"
3,"[2, 1]","['A', 'B']"
CSV文件:

index,similar,markets
1,"[0, 2]","['A', 'C']"
2,"[1, 2]","['B', 'C']"
3,"[2, 1]","['A', 'B']"

您可以使用
pandas
库:

import pandas as pd

x = {'index': [1, 2, 3], 'similar': [[0, 2], [1, 2], [2, 1]], 'markets': [['A', 'C'], ['B', 'C'], ['A', 'B']]}

x = pd.DataFrame(x)
x.to_csv('mycsvfile.csv', index = False)
csv将是:

index,markets,similar
1,"['A', 'C']","[0, 2]"
2,"['B', 'C']","[1, 2]"
3,"['A', 'B']","[2, 1]"

您可以使用
pandas
库:

import pandas as pd

x = {'index': [1, 2, 3], 'similar': [[0, 2], [1, 2], [2, 1]], 'markets': [['A', 'C'], ['B', 'C'], ['A', 'B']]}

x = pd.DataFrame(x)
x.to_csv('mycsvfile.csv', index = False)
csv将是:

index,markets,similar
1,"['A', 'C']","[0, 2]"
2,"['B', 'C']","[1, 2]"
3,"['A', 'B']","[2, 1]"

为什么逗号分隔的值文件不使用逗号分隔其值?为什么要将列表存储在CSV中?你确定你了解CSV是什么以及它们的用途吗?这个任务似乎更适合JSON。为什么逗号分隔的值文件不使用逗号分隔其值?为什么要将列表存储在CSV中?你确定你了解CSV是什么以及它们的用途吗?这个任务似乎更适合JSON。如果a的值发生变化,.keys()列表将以不同的顺序返回!如果a的值更改,a.keys()列表将以不同的顺序返回!