Yaml 在 Python3 版本的 pythonanywhere 中无法正常工作

Yaml not working properly in Python3 version of pythonanywhere

美好的一天。我正在尝试为我的 pythonanywhere 代码创建一个快速配置文件。我尝试使用 YAML,但结果很奇怪。

import os

import yaml

yaml_str = """Time_config:
    Tiempo_entre_avisos: 1
    Tiempo_entre_backups: 7
    Tiempo_entre_pushes: 30
Other_config:
    Morosos_treshold: 800
Mail_config:
    Comunication_mail: ''
    Backup_mail: ''
    Director_mail: []
"""

try:
    yaml_file = open("/BBDD/file.yml", 'w+')
except:
    print("FILE NOT FOUND")
else:
    print("PROCESSING FILE")
    yaml.dump(yaml_str, yaml_file, default_flow_style=False)
    a = yaml.dump(yaml_str, default_flow_style=False)
    print(a) #I make a print to debug
    yaml_file.close()

代码似乎运行良好。但是,结果似乎已损坏。在文件和打印中它看起来像这样(包括“s”):

"Time_config:\n    Tiempo_entre_avisos: 1\n    Tiempo_entre_backups: 7\n    Tiempo_entre_pushes:\  \ 30\nOther_config:\n    Morosos_treshold: 800\nMail_config:\n    Comunication_mail:\  \ ''\n    Backup_mail: ''\n    Director_mail: []\n"

如果我将该字符串复制并粘贴到 python 控制台,yaml 会给我预期的结果,即:

Time_config:
    Tiempo_entre_avisos: 1
    Tiempo_entre_backups: 7
    Tiempo_entre_pushes: 30
Other_config:
    Morosos_treshold: 800
Mail_config:
    Comunication_mail: ''
    Backup_mail: ''
    Director_mail: []

为什么会这样?为什么我没有在第一枪中得到结果?为什么打印换行符 (\n) 而不是插入新行?为什么它包含 " 符号?

我认为您应该先从字符串加载 yaml,然后继续:

# Everything before here is the same
    print("PROCESSING FILE")
    yaml_data = yaml.load(yaml_str)
    yaml.dump(yaml_data, yaml_file, default_flow_style=False)
    a = yaml.dump(yaml_data, default_flow_style=False)
    print(a) #I make a print to debug
    yaml_file.close()