ast.literal_eval() 无法将复杂字符串转换为 Python 中的列表
ast.literal_eval() can't convert complex strings to lists in Python
我正在尝试遍历 GitHub 中存储库中的所有版本,以查找既不是草稿也不是预发行版的最新版本,同时找出总版本数。
我可以通过以下代码获取所有版本的列表:
import requests
print(requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases").text)
但是,这个 returns 一个由多个词典(每个词典代表一个版本)组成的列表,格式为 string。
所以我用谷歌搜索将字符串列表转换为列表对象并找到 this answer。当我使用从上面发出的 GET 请求收到的列表进行尝试时,出现错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "c:\Users\ymzym\OneDrive\Python Projects\GitHub\Encrypt-n-Decrypt\main.pyw", line 731, in <lambda>
self.fileMenu.add_command(label = "Check for updates", accelerator="Ctrl+Alt+U", command=lambda: self.Updates(self), underline=10)
File "c:\Users\ymzym\OneDrive\Python Projects\GitHub\Encrypt-n-Decrypt\main.pyw", line 796, in __init__
print(ast.literal_eval(get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases").text))
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 105, in literal_eval
return _convert(node_or_string)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 85, in _convert
return list(map(_convert, node.elts))
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 104, in _convert
return _convert_signed_num(node)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 78, in _convert_signed_num
return _convert_num(node)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 69, in _convert_num
_raise_malformed_node(node)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 66, in _raise_malformed_node
raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <ast.Name object at 0x00000212D1EFF820>
根据回溯,图书馆显然认为我提供的字符串是 字典。
无论字符串多么复杂,如何将字符串转换为列表?
您可以使用 json
库将字符串解析为复杂的数据类型:
import requests
import json
response_text = requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases").text)
parsed_data = json.loads(response_text)
或者,使用response.json()
直接获取json格式的响应:
import requests
response = requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases")
parsed_data = response.json()
您请求资源以回复 JSON 数据的服务器。您必须将此 JSON 字符串转换为 Python data-structure。您可以使用 json
模块,但幸运的是,requests
有一个方便的功能:.json()
:
import requests
response = requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases")
data = response.json()
print(data)
我正在尝试遍历 GitHub 中存储库中的所有版本,以查找既不是草稿也不是预发行版的最新版本,同时找出总版本数。
我可以通过以下代码获取所有版本的列表:
import requests
print(requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases").text)
但是,这个 returns 一个由多个词典(每个词典代表一个版本)组成的列表,格式为 string。
所以我用谷歌搜索将字符串列表转换为列表对象并找到 this answer。当我使用从上面发出的 GET 请求收到的列表进行尝试时,出现错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "c:\Users\ymzym\OneDrive\Python Projects\GitHub\Encrypt-n-Decrypt\main.pyw", line 731, in <lambda>
self.fileMenu.add_command(label = "Check for updates", accelerator="Ctrl+Alt+U", command=lambda: self.Updates(self), underline=10)
File "c:\Users\ymzym\OneDrive\Python Projects\GitHub\Encrypt-n-Decrypt\main.pyw", line 796, in __init__
print(ast.literal_eval(get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases").text))
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 105, in literal_eval
return _convert(node_or_string)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 85, in _convert
return list(map(_convert, node.elts))
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 104, in _convert
return _convert_signed_num(node)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 78, in _convert_signed_num
return _convert_num(node)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 69, in _convert_num
_raise_malformed_node(node)
File "C:\Users\ymzym\AppData\Local\Programs\Python\Python39\lib\ast.py", line 66, in _raise_malformed_node
raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <ast.Name object at 0x00000212D1EFF820>
根据回溯,图书馆显然认为我提供的字符串是 字典。
无论字符串多么复杂,如何将字符串转换为列表?
您可以使用 json
库将字符串解析为复杂的数据类型:
import requests
import json
response_text = requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases").text)
parsed_data = json.loads(response_text)
或者,使用response.json()
直接获取json格式的响应:
import requests
response = requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases")
parsed_data = response.json()
您请求资源以回复 JSON 数据的服务器。您必须将此 JSON 字符串转换为 Python data-structure。您可以使用 json
模块,但幸运的是,requests
有一个方便的功能:.json()
:
import requests
response = requests.get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases")
data = response.json()
print(data)