【发布时间】:2018-10-17 19:58:36
【问题描述】:
我不知道如何处理 YAML 文件,我有一个包含此内容的 db.yaml 文件
beatport_links:
afro-house: "https://www.beatport.com/genre/afro-house/89/top-100"
big-room: "https://www.beatport.com/genre/big-room/79/top-100"
breaks: "https://www.beatport.com/genre/breaks/9/top-100"
我的程序从这个文件中读取流派名称和前 100 名的链接,然后它会抓取网页中的歌曲名称并将其添加到字典中
def load_yaml_file(self):
with open(self.yaml_file, "r") as file_content:
self.data = yaml.load(file_content)
def get_genres_and_links(self):
for genre, link in self.data.get("beatport_links").items():
self.beatport_links[genre] = link
现在我有一个包含这样内容的列表
["Adam_Beyer_-_Rome_Future_(Original_Mix)", "Veerus_-_Wheel_(Original_Mix)"]
我希望我的程序使用此列表中的内容(附加到它)更新db.yaml 文件,所以最后我希望db.yaml 看起来像这样:
beatport_links:
afro-house: "https://www.beatport.com/genre/afro-house/89/top-100"
big-room: "https://www.beatport.com/genre/big-room/79/top-100"
breaks: "https://www.beatport.com/genre/breaks/9/top-100"
downloaded:
Adam_Beyer_-_Rome_Future_(Original_Mix)
Veerus_-Wheel(Original_Mix)
我该怎么做?
【问题讨论】:
-
你不应该这样使用 PyYAML 的
load(),它被记录为可能不安全。如果必须使用 PyYAML,请使用safe_load()。由于未定义self,因此还缺少一些东西。
标签: python-3.x yaml pyyaml