Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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 使用dict写入csv文件_Python_Python 2.7_Csv_Dictionary - Fatal编程技术网

Python 使用dict写入csv文件

Python 使用dict写入csv文件,python,python-2.7,csv,dictionary,Python,Python 2.7,Csv,Dictionary,我正在使用Python2.7,需要创建一个数据集,为此,我有一个字典,我将把它的内容放在csv文件中。这是我现在的字典: dict = {"arthur1.jpg": [0.123,0.456,0.864], "arthur2.jpg": [0.124,0.764,0.965]} 在csv文件中,它将如下所示: arthur1.jpg 0.123 0.456 0.864 arthur2.jpg 0.124 0.764 0.965 第一列是dicts键的位置,同一行中的下一组列

我正在使用Python2.7,需要创建一个数据集,为此,我有一个字典,我将把它的内容放在csv文件中。这是我现在的字典:

dict = {"arthur1.jpg": [0.123,0.456,0.864], "arthur2.jpg": [0.124,0.764,0.965]}
在csv文件中,它将如下所示:

arthur1.jpg 0.123   0.456   0.864
arthur2.jpg 0.124   0.764   0.965

第一列是dicts键的位置,同一行中的下一组列是相应键的值。

将每个键-值对转换为一行;您的值已经是一个列表,只需在该列表前面加一个包含键的列表文字:

import csv

with open(youcsvfilename, 'rb') as csvf:
    csvwriter = csv.writer(csvf)    
    for filename, values in dictionary.items():
        csvwriter.writerow([filename] + values)

表达式
[filename]+value
在用作输出行的新列表中生成。然后,将该列表以正确的格式写入CSV文件

将每个键值对转换为一行;您的值已经是一个列表,只需在该列表前面加一个包含键的列表文字:

import csv

with open(youcsvfilename, 'rb') as csvf:
    csvwriter = csv.writer(csvf)    
    for filename, values in dictionary.items():
        csvwriter.writerow([filename] + values)

表达式
[filename]+value
在用作输出行的新列表中生成。然后,将该列表以正确的格式写入CSV文件

将每个键值对转换为一行;您的值已经是一个列表,只需在该列表前面加一个包含键的列表文字:

import csv

with open(youcsvfilename, 'rb') as csvf:
    csvwriter = csv.writer(csvf)    
    for filename, values in dictionary.items():
        csvwriter.writerow([filename] + values)

表达式
[filename]+value
在用作输出行的新列表中生成。然后,将该列表以正确的格式写入CSV文件

将每个键值对转换为一行;您的值已经是一个列表,只需在该列表前面加一个包含键的列表文字:

import csv

with open(youcsvfilename, 'rb') as csvf:
    csvwriter = csv.writer(csvf)    
    for filename, values in dictionary.items():
        csvwriter.writerow([filename] + values)

表达式
[filename]+value
在用作输出行的新列表中生成。然后,将该列表以正确的格式写入CSV文件

这是另一种选择:

>>> import pyexcel as pe # pip install pyexcel
>>> dict = {"arthur1.jpg": [0.123,0.456,0.864], "arthur2.jpg": [0.124,0.764,0.965]}
>>> sheet = pe.get_sheet(adict=dict)
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------------+
| arthur1.jpg | arthur2.jpg |
+-------------+-------------+
| 0.123       | 0.124       |
+-------------+-------------+
| 0.456       | 0.764       |
+-------------+-------------+
| 0.864       | 0.965       |
+-------------+-------------+
>>> sheet.transpose()
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------+-------+-------+
| arthur1.jpg | 0.123 | 0.456 | 0.864 |
+-------------+-------+-------+-------+
| arthur2.jpg | 0.124 | 0.764 | 0.965 |
+-------------+-------+-------+-------+
>>> sheet.save_as("my.tsv")
>>> with open("my.tsv") as f:
...     print f.read()
... 
arthur1.jpg 0.123   0.456   0.864
arthur2.jpg 0.124   0.764   0.965

这是另一种选择:

>>> import pyexcel as pe # pip install pyexcel
>>> dict = {"arthur1.jpg": [0.123,0.456,0.864], "arthur2.jpg": [0.124,0.764,0.965]}
>>> sheet = pe.get_sheet(adict=dict)
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------------+
| arthur1.jpg | arthur2.jpg |
+-------------+-------------+
| 0.123       | 0.124       |
+-------------+-------------+
| 0.456       | 0.764       |
+-------------+-------------+
| 0.864       | 0.965       |
+-------------+-------------+
>>> sheet.transpose()
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------+-------+-------+
| arthur1.jpg | 0.123 | 0.456 | 0.864 |
+-------------+-------+-------+-------+
| arthur2.jpg | 0.124 | 0.764 | 0.965 |
+-------------+-------+-------+-------+
>>> sheet.save_as("my.tsv")
>>> with open("my.tsv") as f:
...     print f.read()
... 
arthur1.jpg 0.123   0.456   0.864
arthur2.jpg 0.124   0.764   0.965

这是另一种选择:

>>> import pyexcel as pe # pip install pyexcel
>>> dict = {"arthur1.jpg": [0.123,0.456,0.864], "arthur2.jpg": [0.124,0.764,0.965]}
>>> sheet = pe.get_sheet(adict=dict)
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------------+
| arthur1.jpg | arthur2.jpg |
+-------------+-------------+
| 0.123       | 0.124       |
+-------------+-------------+
| 0.456       | 0.764       |
+-------------+-------------+
| 0.864       | 0.965       |
+-------------+-------------+
>>> sheet.transpose()
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------+-------+-------+
| arthur1.jpg | 0.123 | 0.456 | 0.864 |
+-------------+-------+-------+-------+
| arthur2.jpg | 0.124 | 0.764 | 0.965 |
+-------------+-------+-------+-------+
>>> sheet.save_as("my.tsv")
>>> with open("my.tsv") as f:
...     print f.read()
... 
arthur1.jpg 0.123   0.456   0.864
arthur2.jpg 0.124   0.764   0.965

这是另一种选择:

>>> import pyexcel as pe # pip install pyexcel
>>> dict = {"arthur1.jpg": [0.123,0.456,0.864], "arthur2.jpg": [0.124,0.764,0.965]}
>>> sheet = pe.get_sheet(adict=dict)
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------------+
| arthur1.jpg | arthur2.jpg |
+-------------+-------------+
| 0.123       | 0.124       |
+-------------+-------------+
| 0.456       | 0.764       |
+-------------+-------------+
| 0.864       | 0.965       |
+-------------+-------------+
>>> sheet.transpose()
>>> sheet
Sheet Name: pyexcel_sheet1
+-------------+-------+-------+-------+
| arthur1.jpg | 0.123 | 0.456 | 0.864 |
+-------------+-------+-------+-------+
| arthur2.jpg | 0.124 | 0.764 | 0.965 |
+-------------+-------+-------+-------+
>>> sheet.save_as("my.tsv")
>>> with open("my.tsv") as f:
...     print f.read()
... 
arthur1.jpg 0.123   0.456   0.864
arthur2.jpg 0.124   0.764   0.965

抱歉,但我理解的是:做两行,一行是键,另一行是it值,然后我不理解“prepend”,你的意思是“append”。如果你不介意的话,请你再详细一点好吗?感谢您的关注抱歉,但我理解的是:做两行,一行是键,另一行是it值,那么我不理解“prepend”,您的意思是“append”。如果你不介意的话,请你再详细一点好吗?感谢您的关注抱歉,但我理解的是:做两行,一行是键,另一行是it值,那么我不理解“prepend”,您的意思是“append”。如果你不介意的话,请你再详细一点好吗?感谢您的关注抱歉,但我理解的是:做两行,一行是键,另一行是it值,那么我不理解“prepend”,您的意思是“append”。如果你不介意的话,请你再详细一点好吗?谢谢你的关注