简体   繁体   English

如何将json文件读入python?

[英]How do I read a json file into python?

I'm new to JSON and Python, any help on this would be greatly appreciated.我是 JSON 和 Python 的新手,对此的任何帮助将不胜感激。

I read about json.loads but am confused我读过 json.loads 但很困惑

How do I read a file into Python using json.loads?如何使用 json.loads 将文件读入 Python?

Below is my JSON file format:以下是我的 JSON 文件格式:

{
        "header": {
        "platform":"atm"
        "version":"2.0"
       }
        "details":[
       {
        "abc":"3"
        "def":"4"
       },
       {
        "abc":"5"
        "def":"6"
       },
       {
        "abc":"7"
        "def":"8"
       }    
      ]
    }

My requirement is to read the values of all "abc" "def" in details and add this is to a new list like this [(1,2),(3,4),(5,6),(7,8)] .我的要求是详细读取所有"abc" "def"的值并将其添加到这样的新列表中[(1,2),(3,4),(5,6),(7,8)] The new list will be used to create a spark data frame.新列表将用于创建 spark 数据框。

Open the file, and get a filehandle:打开文件,获取文件句柄:

fh = open('thefile.json')

https://docs.python.org/2/library/functions.html#open https://docs.python.org/2/library/functions.html#open

Then, pass the file handle into json.load(): (don't use loads - that's for strings)然后,将文件句柄传递给 json.load():(不要使用负载 - 那是用于字符串)

import json
data = json.load(fh)

https://docs.python.org/2/library/json.html#json.load https://docs.python.org/2/library/json.html#json.load

From there, you can easily deal with a python dictionary that represents your json-encoded data.从那里,您可以轻松处理表示 json 编码数据的 Python 字典。

new_list = [(detail['abc'], detail['def']) for detail in data['details']]

Note that your JSON format is also wrong.请注意,您的 JSON 格式也是错误的。 You will need comma delimiters in many places, but that's not the question.您将在许多地方需要逗号分隔符,但这不是问题。

I'm trying to understand your question as best as I can, but it looks like it was formatted poorly.我试图尽可能地理解你的问题,但看起来它的格式很差。

First off your json blob is not valid json, it is missing quite a few commas.首先,您的 json blob 不是有效的 json,它缺少很多逗号。 This is probably what you are looking for:这可能是您正在寻找的:

{
    "header": {
        "platform": "atm",
        "version": "2.0"
    },
    "details": [
        {
            "abc": "3",
            "def": "4"
        },
        {
            "abc": "5",
            "def": "6"
        },
        {
            "abc": "7",
            "def": "8"
        }
    ]
}

Now assuming you are trying to parse this in python you will have to do the following.现在假设您尝试在 python 中解析它,您将必须执行以下操作。

import json

json_blob = '{"header": {"platform": "atm","version": "2.0"},"details": [{"abc": "3","def": "4"},{"abc": "5","def": "6"},{"abc": "7","def": "8"}]}'
json_obj = json.loads(json_blob)

final_list = []

for single in json_obj['details']:
    final_list.append((int(single['abc']), int(single['def'])))

print(final_list)

This will print the following: [(3, 4), (5, 6), (7, 8)]这将打印以下内容:[(3, 4), (5, 6), (7, 8)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM