2 回答

TA贡献1853条经验 获得超18个赞
您必须使用 library 将生成的搅拌解析为字典json,然后您可以通过循环遍历 json 结构来遍历结果,如下所示:
import json
raw_output = '{"ParsedResults": [ { "Tex...' # your api response
json_output = json.loads(raw_output)
# iterate over all lists
phone_numbers = []
for parsed_result in json_output["ParsedResults"]:
for line in parsed_result["TextOverlay"]["Lines"]:
# now add all phone numbers in "Words"
phone_numbers.extend([word["WordText"] for word in line["Words"]])
print(phone_numbers)
您可能想要检查该进程中是否存在所有密钥,具体取决于您使用的 API,例如
# ...
for line in parsed_result["TextOverlay"]["Lines"]:
if "Words" in line: # make sure key exists
phone_numbers.extend([word["WordText"] for word in line["Words"]])
# ...

TA贡献1809条经验 获得超8个赞
将 API 响应存储到变量。让我们称之为response。
现在使用json模块将 JSON 字符串转换为 Python 字典。
import json
response_dict = json.loads(response)
现在遍历response_dict以获取所需的文本。
phone_number = response_dict["ParsedResults"][0]["TextOverlay"]["Lines"][0]["Words"][0]["WordText"]
只要字典值是数组,[0]就用于访问数组的第一个元素。如果要访问数组的所有元素,则必须遍历数组。
添加回答
举报