json>csv使用in2cv指定键不返回值

2024-10-03 17:19:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用xml2json将xml文件转换为json。在

它的一小部分如下所示。我想把这个转换成csv。我正在使用csvkit in2csv

使用基本语法会显示一个错误,很简单。在

C:\Users\Renshaw\Documents\Sayth\XML>in2csv test2.json > test2.csv
When converting a JSON document with a top-level dictionary element, a key must
be specified.

所以加上键我没有错误,但也没有输出。在

^{pr2}$

我已经尝试了广泛的键现在没有错误,但也没有输出,有什么想法,如何使它提供输出的csv?在

{
  "Meeting": {
    "NumOfRaces": {
      "#tail": "\n  ",
      "#text": "9"
    },
    "WeightsPublishing": {
      "#tail": "\n  ",
      "#text": "2014-09-30T00:00:00+10:00"
    },
    "NominationsClose": {
      "#tail": "\n  ",
      "#text": "2014-09-29T12:00:00+10:00"
    },
    "CodeType": {
      "#tail": "\n  ",
      "#text": "GALLOPS"
    },
    "Track": {
      "Rainfall": {
        "#tail": "\n    ",
        "#text": "Nil last 24hrs, 4.2mm last 7 days"
      },
      "Irrigation": {
        "#tail": "\n    ",
        "#text": "Nil last 24hrs, 25mm last 7 days"
      },
      "RailPosition": {
        "#tail": "\n    ",
        "#text": "+9m Entire Circuit"
      },
      "#tail": "\n  ",
      "TrackSurface": {
        "#tail": "\n    ",
        "#text": "Turf"
      },
      "Comments": {
        "#tail": "\n    ",
        "#text": "Finalised 4\/10 - 7:45am  Late Scratching Race 3 No. 4"
      },
      "Weather": {
        "#tail": "\n    ",
        "#text": "Fine"
      },
      "Penetrometer": {
        "#tail": "\n    ",
        "#text": "4.83"
      },
      "RailPositionLastMeeting": {
        "#tail": "\n    ",
        "#text": "True Position Entire Circuit"
      },
      "TrackInfo": {
        "#tail": "\n  ",
        "#text": "Penetrometer: Inside 4.85, Outside 4.85"
      },
      "TrackRating": {
        "#tail": "\n    ",
        "#text": "Good"
      },
      "#text": "\n    ",
      "RacingDirection": {
        "#tail": "\n    ",
        "#text": "AntiClockwise"
      }
    },
    "MeetingStage": {
      "#tail": "\n  ",
      "#text": "Acceptances"
    },
    "Races": {
      "#tail": "\n",
      "#text": "\n    ",
      "Race": [
        {
          "Comments": {
            "#tail": "\n    "
          },
          "NominationsDivisor": {
            "#tail": "\n      ",
            "#text": "0"
          },
          "Starters": {
            "#tail": "\n      ",
            "#text": "11"
          },
          "TrackRecords": {
            "#tail": "\n      ",
            "TrackRecord": {
              "TrackRecordHorse": {
                "#tail": "\n        "
              },
              "#text": "\n          ",
              "#tail": "\n      ",
              "DistanceRace": {
                "#tail": "\n          ",
                "#text": "1000"
              },
              "Time": {
                "#tail": "\n          ",
                "#text": "00:00:55.420"
              },
              "RaceNumber": {
                "#tail": "\n          ",
                "#text": "7"
              },
              "RaceDate": {
                "#tail": "\n          ",
                "#text": "2013-02-16"
              }
            },
            "#text": "\n        "
          },
          "RaceDistance": {
            "#tail": "\n      ",
            "#text": "1000"
          },
          "NominationsRaceNumber": {
            "#tail": "\n      ",
            "#text": "1"
          },
          "ApprenticeCanClaim": {
            "#tail": "\n      ",
            "#text": "false"
          },
          "SizeField": {
            "#tail": "\n      ",
            "#text": "16"
          },
          "NameRaceForm": {
            "#tail": "\n      ",
            "#text": "MARIBYRNONG TRL"
          },
          "RaceType": {
            "#tail": "\n      ",
            "#text": "Flat"
          },
          "SizeEmergency": {
            "#tail": "\n      ",
            "#text": "4"
          },
          "DistanceApprox": {
            "#tail": "\n      ",
            "#text": "false"
          },
          "#text": "\n      ",
          "BallotedOutEntries": {
            "#tail": "\n      "
          },
          "Logos": {
            "#tail": "\n      ",
            "Logo": {
              "#tail": "\n      "
            },
            "#text": "\n        "
          },
          "#tail": "\n    ",
          "TrackCircumference": {
            "#tail": "\n      ",
            "#text": "2313"
          },
          "NameRaceNews": {
            "#tail": "\n      ",
            "#text": "Maribyrnong Trial Stakes"
          },
          "WeightChange": {
            "#tail": "\n      ",
            "#text": "0.00"
          },
          "Accepters": {
            "#tail": "\n      ",
            "#text": "12"
          },
          "RaceEntries": {
            "RaceEntry": [
              {
                "Trainer": {
                  "Location": {
                    "#tail": "\n            ",
                    "#text": "Cranbourne"
                  },
                  "#text": "\n            ",
                  "Surname": {
                    "#tail": "\n            ",
                    "#text": "Laing"

Tags: csvtextjson错误dayscommentstaillast
2条回答

你所做的有两个问题。在

首先,您错误地指定了键(在本例中,当您处理JSON时,使用的是XML/XPath样式,带有斜杠)。您只需提供元素的名称(例如Meeting)。在

但是,主要的问题是您使用的JSON类型,它由多个嵌套字典组成,in2cv无法真正处理这些问题(对于多个级别,它如何知道要使用哪些列?)。您需要以某种方式将数据展平,以便可以清楚地标识字段。在

您可以研究一下this question,了解如何将JSON转换成CSV,因为我不认为in2csv会在您的案例中切断它。在

如果要将每个XML路径转换为路径表达式, 在CSV的第1列中使用它, 并使用列2最底层的值, 以下代码可以解决您的问题:

import json

json_input = """{
  "Meeting": {
    "NominationsClose": {
      "#tail": "\\n  ",
      "#text": "2014-09-29T12:00:00+10:00"
    },
    "CodeType": {
      "#tail": "\\n  ",
      "#text": "GALLOPS"
    },
    "Track": {
      "Rainfall": {
        "#tail": "\\n    ",
        "#text": "Nil last 24hrs, 4.2mm last 7 days"
      },
      "Irrigation": {
        "#tail": "\\n    ",
        "#text": "Nil last 24hrs, 25mm last 7 days"
      }
    }
  }
}"""

def print_csv_depth_first(tree, path=""):
    if isinstance(tree, dict):
        for key in tree.keys():
            print_csv_depth_first(tree[key], "{}/{}".format(path, key))
    elif isinstance(tree, list):
        for i in range(len(tree)):
            print_csv_depth_first(tree[i], "{}/{}".format(path, str(i)))
    elif isinstance(tree, str):
        entry = tree
        print('{},{}'.format(path, repr(entry)))
        return

json = json.loads(json_input)
print_csv_depth_first(json)

我已经包含了示例JSON数据的一小部分。 在最底部,您的数据还包含列表的开头,"RaceEntry": [, 但这是不完整的,所以我不得不推断。 上述代码生成以下输出:

^{pr2}$

您必须调整包含print语句的行以满足您的需要。在

相关问题 更多 >