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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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字符串应该在字典中结束_Python_Python 3.x_String_Dictionary_Error Handling - Fatal编程技术网

Python字符串应该在字典中结束

Python字符串应该在字典中结束,python,python-3.x,string,dictionary,error-handling,Python,Python 3.x,String,Dictionary,Error Handling,我试图让这个字符串在字典中结束,但我收到下面的错误消息。你知道怎么解决这个问题吗?谢谢 我的代码: import ast str = '{"success":true,"timestamp":1617349986,"base":"EUR","date":"2021-04-02","rates":{"USD":1.177996}}' d =

我试图让这个字符串在字典中结束,但我收到下面的错误消息。你知道怎么解决这个问题吗?谢谢

我的代码:

import ast
str = '{"success":true,"timestamp":1617349986,"base":"EUR","date":"2021-04-02","rates":{"USD":1.177996}}'
d = ast.literal_eval(str)
错误:

Traceback (most recent call last):
  File "C:\Users\xh\Documents\Other und Programme\Programme\PyCharm Community Edition 2018.3.1\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py", line 91, in literal_eval
    return _convert(node_or_string)
  File "C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py", line 79, in _convert
    map(_convert, node.values)))
  File "C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py", line 90, in _convert
    return _convert_signed_num(node)
  File "C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py", line 63, in _convert_signed_num
    return _convert_num(node)
  File "C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py", line 55, in _convert_num
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x00000215DF9CA248>
回溯(最近一次呼叫最后一次):
文件“C:\Users\xh\Documents\Other und Program\Program\PyCharm Community Edition 2018.3.1\helpers\pydev\\u pydevd\u bundle\pydevd\u exec2.py”,第3行,在Exec中
执行官(exp、全局变量、本地变量)
文件“”,第1行,在
文件“C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py”,第91行,文本值
返回\u转换(节点\u或字符串)
文件“C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py”,第79行,在转换中
映射(_convert,node.values)))
文件“C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py”,第90行,在转换中
return\u convert\u signed\u num(节点)
文件“C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py”,第63行,在\u convert\u signed\u num中
返回_convert_num(节点)
文件“C:\Users\xh\AppData\Local\Programs\Python\Python37\lib\ast.py”,第55行,在convert\u num中
raise VALUERROR('格式错误的节点或字符串:'+repr(节点))
ValueError:节点或字符串格式不正确:

您拥有的是有效的JSON。不要使用
ast.literal\u eval
,使用
json
模块

import json
true
spam = '{"success":true,"timestamp":1617349986,"base":"EUR","date":"2021-04-02","rates":{"USD":1.177996}}'
eggs = json.loads(spam)
print(eggs)

作为旁注-不要使用
str
作为名称-它是一个内置函数。

您的字符串似乎是JSON格式的

使用
json
模块:

import json

input_str = '{"success":true,"timestamp":1617349986,"base":"EUR","date":"2021-04-02","rates":{"USD":1.177996}}'

input_dict = json.loads(input_str)
print(input_dict)
其中:


{'success':True,'timestamp':1617349986,'base':'EUR','date':'2021-04-02','rates':{'USD':1.177996}

您的字符串无效。它看起来像JSON。为什么您认为它是Python文字?非常感谢。解决了我的问题。非常感谢。解决了我的问题。