Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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文件创建JSON_Python_Json_Csv - Fatal编程技术网

Python 从CSV文件创建JSON

Python 从CSV文件创建JSON,python,json,csv,Python,Json,Csv,当我打印数据时,我得到的输出是 I have a csv file like this RPM,Load Current,Output 1200,3,12 1500,4,13 And I have to create a json file in this format SpikeData:{ "RPM" = [1200,1500], "Load Current" = [3,4], "Output" = [12,13] } I have written the following code

当我打印数据时,我得到的输出是

I have a csv file like this
RPM,Load Current,Output
1200,3,12
1500,4,13

And I have to create a json file in this format

SpikeData:{
"RPM" = [1200,1500],
"Load Current" = [3,4],
"Output" = [12,13]
}

I have written the following code to read the csv file and convert it to 
json.

import csv
import json

#Read CSV File
def read_csv():
    SpikeData={"RPM":[],"Load Current":[],"Output":[]}

    with open('power1.txt') as CSV_file_ref:
        Reader = csv.DictReader(CSV_file_ref)
        columns_of_first_line = Reader.fieldnames
        for row in Reader:
        SpikeData['RPM'].append(row[i] for i in range(len(column_of_first_line)))   
        print(SpikeData)

if __name__ == "__main__":
   read_csv()
{
“LoadCurrent”:[],
“BatteryOutput”:[],
“功率容量”:[],
“RPM”:[,,
,
]
}
谁能帮我一下吗

我希望值保存在“RPM”中,比如“RPM”=[12001500]

但是保存的值是

    {
    'LoadCurrent': [],
    'BatteryOutput': [], 
    'PowerCapacity': [],
    'RPM': [<generator object <genexpr> at 0x0000000005B8A7E0>,
     <generator object <genexpr> at 0x0000000005BE07E0>,
     <generator object <genexpr> at 0x0000000005BE0630>]
   }
“RPM”:[,,
,
]

您需要稍微更改代码,不需要生成器表达式

 'RPM': [<generator object <genexpr> at 0x0000000005B8A7E0>, 
  <generator object <genexpr> at 0x0000000005BE07E0>,
  <generator object <genexpr> at 0x0000000005BE0630>]
输出:

import csv
import json

#Read CSV File
def read_csv():
    SpikeData={"RPM":[],"Load Current":[],"Output":[]}

    with open('power1.txt') as CSV_file_ref:
        Reader = csv.DictReader(CSV_file_ref)
        for row in Reader:
            SpikeData['RPM'].append(row['RPM'])
            SpikeData['Load Current'].append(row['Load Current'])
            SpikeData['Output'].append(row['Output'])
        print(SpikeData)

if __name__ == "__main__":
   read_csv()
输出:

#csv file content
----------------------
RPM,Load Current,Output
1200,3,12
1500,4,13
--------------------

code:

import pandas as pd
import json
df = pd.read_csv('sample.csv')
csv_file = pd.DataFrame(pd.read_csv("sample.csv", sep = ",", header = 0, index_col = False))
data = json.loads(csv_file.to_json())
print data

.append(行[i]表示范围内的i(len(第一行的列))
是一个生成器表达式,因此当然,您的
dict
包含生成器。谢谢@Shivam Singh。我的代码现在给出了所需的输出欢迎,如果有帮助,请将此标记为正确答案。done@shivam singh
#csv file content
----------------------
RPM,Load Current,Output
1200,3,12
1500,4,13
--------------------

code:

import pandas as pd
import json
df = pd.read_csv('sample.csv')
csv_file = pd.DataFrame(pd.read_csv("sample.csv", sep = ",", header = 0, index_col = False))
data = json.loads(csv_file.to_json())
print data
{"RPM":{"0":1200,"1":1500},"Load Current":{"0":3,"1":4},"Output":{"0":12,"1":13}}