Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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文件?_Python_Json_Python 2.7 - Fatal编程技术网

如何使用python打开json文件?

如何使用python打开json文件?,python,json,python-2.7,Python,Json,Python 2.7,ENG_RequestData.json: { "appKey": "9c9fa7201e90d3d96718bc3f36ce4cfe1781f2e82f4e5792996623b3b474fee2c77699eb5354f2136063e1ff19c378f0f6dd984471a38ca5c393801bffb062d6", "appId": "NMDPTRIAL_AutomotiveTesting_NCS61HTTP", "uId": "Alexander",

ENG_RequestData.json:

{
    "appKey": "9c9fa7201e90d3d96718bc3f36ce4cfe1781f2e82f4e5792996623b3b474fee2c77699eb5354f2136063e1ff19c378f0f6dd984471a38ca5c393801bffb062d6",
    "appId": "NMDPTRIAL_AutomotiveTesting_NCS61HTTP",
    "uId": "Alexander",
    "inCodec": "PCM_16_8K",
    "outCodec": "PCM_16_8K",
    "cmdName": "NVC_TTS_CMD",
    "appName": "Python",
    "appVersion": "1",
    "language": "eng-GB",
    "carrier": "carrier",
    "deviceModel": "deviceModel",
    "cmdDict": {
        "tts_voice": "Serena",
        "tts_language": "eng-GB",
        "locale": "canada",
        "application_name": "Testing Python Script",
        "organization_id": "NUANCE",
        "phone_OS": "4.0",
        "phone_network": "wifi",
        "audio_source": "SpeakerAndMicrophone",
        "location": "47.4925, 19.0513",
        "application_session_id": "1234567890",
        "utterance_number": "5",
        "ui_langugage": "en",
        "phone_submodel": "nmPhone2,1",
        "application_state_id": "45"        
        }
}
代码:

    print LNG    // it is printing as ENG
    ENG_RequestDataFile = scriptPath + "\\" + "ENG_RequestData.json"
    print ENG_RequestDataFile  // it is printing as C:\Users\\Desktop\OWN\2016_02_11\ENG_RequestData.json
    DEU_RequestDataFile = scriptPath + "\\" + "DEU_RequestData.json"
    try:
        if LNG == 'ENG':
            with open(ENG_RequestDataFile) as json_file:   
                print json_file
                JSON_ENGData = json.load(json_file)
                print JSON_ENGData
        elif LNG == 'DEU':
            with open(DEU_RequestDataFile) as json1_file:    
                JSON_DEUData = json.load(json1_file)
        else:
            print ("No Other Language")
    except:
        print ("[ERROR] Cannot open the Request data file")
我正在从特定路径读取一个json文件,json文件如上图所示。有两个json文件,一个是英语和德语,但我正在尝试读取,但它正在打印,因为[ERROR]无法打开请求数据文件。我打不开它。有人能告诉我怎么做吗?

试试下面的代码:

import json

#Path you posted
path = os.path.join('C:'+os.sep+'Users'+os.sep+'Desktop'
                    +os.sep+'OWN'+os.sep+'2016_02_11'
                    +os.sep+'ENG_RequestData.json')

def get_tts(LNG,filename):
    try:
        if LNG == 'ENG':
            with open(filename) as json_file:    
                JSON_ENGData = json.load(json_file)
                print(JSON_ENGData)
        elif LNG == 'DEU':
            with open(DEU_RequestDataFile) as json_file:    
                JSON_DEUData = json.load(json_file)
        else:
            print("No Other Language")
    except:
        print("[ERROR] Cannot open the Request data file")

#Execute the function
get_tts('ENG',path)
更多信息请点击此处:

我正在使用try和except在代码中打开json文件,但我无法做到这一点。错误是什么?@sam是get_tts函数中的scriptpath变量未定义。它不在try和except循环中。你能告诉我为什么吗?路径是正确的,我通过在代码中打印来检查。在执行代码之前是否导入了json模块?我添加了导入json。你是在说别的吗?请把这个精简成一个。