Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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/8/http/4.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
如何在Python2.7中重新构造字典序列?_Python_Json_Python 2.7_Dictionary - Fatal编程技术网

如何在Python2.7中重新构造字典序列?

如何在Python2.7中重新构造字典序列?,python,json,python-2.7,dictionary,Python,Json,Python 2.7,Dictionary,我已通过以下过程读取CSV文件并将其构建为字典格式: with open("data.csv") as csvfile: reader = csv.DictReader(csvfile) 我试图做的是构建一个for循环,该循环通过这个字典系列进行解析,并将(.stdout)写入一个新行分隔(\n)的JSON系列逐系列表示,如下所示: { "State":"CA", "points":[ ["2015-01-01", 33.5], ["201

我已通过以下过程读取CSV文件并将其构建为字典格式:

with open("data.csv") as csvfile: 
    reader = csv.DictReader(csvfile)
我试图做的是构建一个for循环,该循环通过这个字典系列进行解析,并将(.stdout)写入一个新行分隔(\n)的JSON系列逐系列表示,如下所示:

  {
    "State":"CA",
    "points":[
      ["2015-01-01", 33.5],
      ["2016-01-01", 100.8],
      ["2018-01-01", 10.1]
    ],
    "fields":{
      "Product":"TA",
      "Line":"V1"
    }
  } 
这是csv文件的一个示例:

STATE  DATE    VALUE PRODUCT LINE TRAN
CA    2015-01  33.5   TA      V1   4
CA    2015-05  31.7   TA      V1   4
CA    2016-01  100.8  TA      V1   4
FL    2017-01  93.5   LC      M2   2
FL    2017-03  150.5  LC      M2   4
NY    2013-01  50.4   AB      AB   1
NY    2016-01  70.5   AB      AB   1
这是使用(DictReader)后获得的字典序列的一个示例:

我被困在for循环的中间部分,我找不到重组到这个新jason序列的方法:

with open("data.csv") as csvfile: 
    reader = csv.DictReader(csvfile)

for row in reader:
        for key in row:


new_jsonsqc = json.dumps(row)
sys.stdout.write(new_jsonsqc + '\n')

非常感谢你的帮助

只需检查键并相应地附加到新的\u jsonsqc:

import csv
import json
import sys

with open("data.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        new_jsonsqc = {'points': [], 'fields': {}}
        for key in row:
            if key == 'STATE':
                new_jsonsqc['State'] = row[key]
            if key == 'DATE':
                new_jsonsqc['points'].append(row[key])
            if key == 'VALUE':
                new_jsonsqc['points'].append(row[key])
            if key == 'PRODUCT':
                new_jsonsqc['fields']['Product'] = row[key]
            if key == 'LINE':
                new_jsonsqc['fields']['Line'] = row[key]
        sys.stdout.write(json.dumps(new_jsonsqc) + '\n')

你想要的json序列是什么?我想要一个序列接一个序列,就像上面的序列,里面有唯一的id“State”、“points”和“fields”键。
import csv
import json
import sys

with open("data.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        new_jsonsqc = {'points': [], 'fields': {}}
        for key in row:
            if key == 'STATE':
                new_jsonsqc['State'] = row[key]
            if key == 'DATE':
                new_jsonsqc['points'].append(row[key])
            if key == 'VALUE':
                new_jsonsqc['points'].append(row[key])
            if key == 'PRODUCT':
                new_jsonsqc['fields']['Product'] = row[key]
            if key == 'LINE':
                new_jsonsqc['fields']['Line'] = row[key]
        sys.stdout.write(json.dumps(new_jsonsqc) + '\n')