何在python中处理不规则的json文件以获取CSV中的序列化

何在python中处理不规则的json文件以获取CSV中的序列化

{\n      \"1 & 1 Internet\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 55,\n        \"abc\": \"low\"\n      },\n      \"1 website hosting\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"10000ft\": {\n        \"category\": \"Collaboration\",\n        \"xyz\": 48,\n        \"abc\": \"poor\"\n      },\n      \"1010data\": {\n        \"category\": \"Big Data\",\n        \"xyz\": 56,\n        \"abc\": \"low\"\n      },\n      \"101domains\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"111WebHost.com\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 6,\n        \"abc\": \"poor\"\n      },\n      \"11Giraffes\": {\n        \"category\": \"Web Design\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"123 FlashChat\": {\n        \"category\": \"Collaboration\",\n        \"xyz\": 64,\n        \"abc\": \"medium\"\n      },\n      \"123-Hosts\": {\n        \"category\": \"Web Design & Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"123-reg\": {\n        \"category\": \"Web Design & Infrastructure\",\n        \"xyz\": 52,\n        \"abc\": \"low\"\n      },\n      \"123ContactForm\": {\n        \"category\": \"Survey Solutions\",\n        \"xyz\": 43,\n        \"abc\": \"poor\"\n      },\n      \"123LandLord\": {\n        \"category\": \"Asset Management\",\n        \"xyz\": 18,\n        \"abc\": \"poor\"\n      },\n      \"123connect\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 8,\n        \"abc\": \"poor\"\n      },\n      \"15Five\": {\n        \"category\": \"Telecom\",\n        \"xyz\": 49,\n        \"abc\": \"poor\"\n      },\n      \"15MinuteServers\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 13,\n        \"abc\": \"poor\"\n      },\n      \"1ShoppingCart\": {\n        \"category\": \"Software Development\",\n        \"xyz\": 27,\n        \"abc\": \"poor\"\n      },\n      \"1Web Hosting\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 3,\n        \"abc\": \"poor\"\n      },\n      \"1st Domains\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 19,\n        \"abc\": \"poor\"\n      },\n      \"1st Easy\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 51,\n        \"abc\": \"low\"\n      },\n      \"1st4webshops\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 12,\n        \"abc\": \"poor\"\n      },\n      \"1stDNS Limited\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"1time\": {\n        \"category\": \"Expense Management\",\n        \"xyz\": 12,\n        \"abc\": \"poor\"\n      },\n      \"2001 Web\": {\n        \"category\": \"Web Design & Infrastructure\",\n        \"xyz\": 24,\n        \"abc\": \"poor\"\n      },\n      \"23Video\": {\n        \"category\": \"Content Management\",\n        \"xyz\": 31,\n        \"abc\": \"poor\"\n      },\n      \"24 Seven Office\": {\n        \"category\": \"Enterprise Resource Planning\",\n        \"xyz\": 14,\n        \"abc\": \"poor\"\n      }


输出:

1&1互联网,基础设施,55,低

1个网站托管,基础架构,0,差

...............................................

...............................................

以此类推

最佳答案

Python 2.7.8 (default, Oct 20 2014, 15:05:19)
>>> data = """{\n      \"1 & 1 Internet\": {
...              \n        \"category\": \"Infrastructure\",
...              \n        \"xyz\": 55,
...              \n        \"ABC\": \"low\"
...              \n      },
...   \n      \"1 website hosting\": {
...              \n        \"category\": \"Infrastructure\",
...              \n        \"xyz\": 0,
...              \n        \"ABC\": \"poor\"
...              \n      },
...   \n      \"10000ft\": {
...              \n        \"category\": \"Collaboration\",
...              \n        \"xyz\": 48,
...              \n        \"ABC\": \"poor\"\n
... }"""
>>> import json
>>> json.loads(data)
...
ValueError: Expecting object: line 30 column 1 (char 489)


缺少右花括号:

>>> data = json.loads(data+'}')
>>> for key, value in data.iteritems():
...     print key, value['category'], value['xyz'], value['ABC']
...
1 & 1 Internet Infrastructure 55 low
10000ft Collaboration 48 poor
1 website hosting Infrastructure 0 poor

关于python - 如何在python中处理不规则的json文件以获取CSV中的序列化值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30884493/

10-10 16:53