【发布时间】:2020-04-02 05:16:28
【问题描述】:
我将来自 epguide API 的节目数据保存在一个 txt 文件中,如下所示:
[{'epguide_name': 'gooddoctor', 'title': '好医生', 'imdb_id': 'tt6470478', 'episodes': 'http://epguides.frecar.no/show/gooddoctor/', 'first_episode': 'http://epguides.frecar.no/show/gooddoctor/first/' , 'next_episode': 'http://epguides.frecar.no/show/gooddoctor/next/', 'last_episode': 'http://epguides.frecar.no/show/gooddoctor/last/', 'epguides_url': 'http://www.epguides.com/gooddoctor'}]
当我现在尝试在 python 中将其读取为列表时,它不会将其识别为这样,而只是将其识别为字符串,尽管有方括号:
with open(file_shows, 'r', encoding='utf-8') as fs:
fs = fs.read()
print(type(fs))
print(next((item for item in list if item['title'] == show), None)['episodes'])
Type 仍然是 str,因此搜索也不起作用。如何将数据“返回”到列表?
【问题讨论】:
-
您使用
fs.read()阅读的任何内容都会以字符串形式返回。它不会尝试解析为 Python 表达式。 -
谢谢汤姆,还有其他选择吗?
标签: python string list square-bracket