Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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使json文件成为有效的json文件_Json_Python 3.x - Fatal编程技术网

如何使用Python使json文件成为有效的json文件

如何使用Python使json文件成为有效的json文件,json,python-3.x,Json,Python 3.x,我是Python新手,我的Json文件如下所示: [ { "Symbol": "TCS", "Series": "EQ", "Date": "04-May-2020", "Prev Close": 2014.45, "Open Price": 1966

我是Python新手,我的Json文件如下所示:

[
    {
        "Symbol": "TCS",
        "Series": "EQ",
        "Date": "04-May-2020",
        "Prev Close": 2014.45,
        "Open Price": 1966.0,
        "High Price": 1966.0,
        "Low Price": 1913.65,
        "Last Price": 1930.5,
        "Close Price": 1930.45,
        "Average Price": 1939.3,
        "Total Traded Quantity": 3729409.0,
        "Turnover": 7232442404.05,
        "No. of Trades": 165528.0,
        "Deliverable Qty": 1752041.0,
        "% Dly Qt to Traded Qty": 46.98
    }
]
应该是这样的

{ 


"tcs":[
    {
        "Symbol": "TCS",
        "Series": "EQ",
        "Date": "04-May-2020",
        "Prev Close": 2014.45,
        "Open Price": 1966.0,
        "High Price": 1966.0,
        "Low Price": 1913.65,
        "Last Price": 1930.5,
        "Close Price": 1930.45,
        "Average Price": 1939.3,
        "Total Traded Quantity": 3729409.0,
        "Turnover": 7232442404.05,
        "No. of Trades": 165528.0,
        "Deliverable Qty": 1752041.0,
        "% Dly Qt to Traded Qty": 46.98
    }
]
}

如何通过Python修改它?

如果json文件名为
data.json
,则可以使用以下脚本:

import json

with open('data.json', 'r') as f_in:
    data = json.load(f_in)

with open('data_out.json', 'w') as f_out:
    json.dump({'tcs': data}, f_out, indent=4)
输出将是
data\u out.json
,内容如下:

{
    "tcs": [
        {
            "Symbol": "TCS",
            "Series": "EQ",
            "Date": "04-May-2020",
            "Prev Close": 2014.45,
            "Open Price": 1966.0,
            "High Price": 1966.0,
            "Low Price": 1913.65,
            "Last Price": 1930.5,
            "Close Price": 1930.45,
            "Average Price": 1939.3,
            "Total Traded Quantity": 3729409.0,
            "Turnover": 7232442404.05,
            "No. of Trades": 165528.0,
            "Deliverable Qty": 1752041.0,
            "% Dly Qt to Traded Qty": 46.98
        }
    ]
}
def updateJsonContent():
    jsonFile = open("your_json_file.json", "r") # Open the JSON file for reading
    data = json.load(jsonFile)  
    jsonFile.close()  

    updated_data = {"tcs":data}

    # Save the changes to JSON file
    jsonFile = open("your_json_file.json", "w+")
    jsonFile.write(json.dumps(updated_data))
    jsonFile.close()