我无法正确导入此json文件

时间:2018-10-19 01:06:29

标签: python json file import path

if __name__ == '__main__':
    import json

    data = json.load(open('C:\\Users\\lenovo\\Documents\\FinPy\\FinPy\\Data\\letras.json)')

print (data)

编辑: 错误消息如下:

runfile('C:/Users/lenovo/Documents/FinPy/FinPy/test.py', wdir='C:/Users/lenovo/Documents/FinPy/FinPy')
Traceback (most recent call last):

  File "C:\Users\lenovo\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-1-699848768d77>", line 1, in <module>
    runfile('C:/Users/lenovo/Documents/FinPy/FinPy/test.py', wdir='C:/Users/lenovo/Documents/FinPy/FinPy')

  File "C:\Users\lenovo\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\lenovo\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/lenovo/Documents/FinPy/FinPy/test.py", line 40
    #dict_tir = {n: tir(i) for n, i in data['mercado'].items()}
                                                               ^
SyntaxError: unexpected EOF while parsing

letras.json具有以下内容:

{
    "mercado" : {
        "LELIQ" : { "precio" :  98.63, "capital" : 100.0000, "plazo" :   7 },
        "I21N8" : { "precio" :  94.40, "capital" : 100.0000, "plazo" :  42 },
        "LTPN8" : { "precio" : 103.00, "capital" : 110.6814, "plazo" :  51 },
        "I19D8" : { "precio" :  90.25, "capital" : 100.0000, "plazo" :  70 },
        "LTPD8" : { "precio" : 102.75, "capital" : 114.5642, "plazo" :  79 },
        "LTPE9" : { "precio" : 101.90, "capital" : 118.5253, "plazo" : 113 },
        "LTPF9" : { "precio" : 101.15, "capital" : 122.7838, "plazo" : 141 },
        "LTPM9" : { "precio" :  98.00, "capital" : 123.1152, "plazo" : 170 },
        "LTPS9" : { "precio" : 102.77, "capital" : 151.7697, "plazo" : 355 }
    },

    "portfolio" : {
        "LTPN8" : 0.3,
        "LTPD8" : 0.2,
        "LTPE9" : 0.2,
        "LTPF9" : 0.1,
        "LTPM9" : 0.1,
        "LTPS9" : 0.1
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下。

import json
F = open("C:\RUTE\YOUR\FILE","r") 
print F, "this is your file"
saved = F.read()
data = json.loads(saved)

print (data["mercado"])

Python v2.7.13 (我不太懂英语,所以这就是为什么我的评论简短)