【发布时间】:2016-03-01 16:42:56
【问题描述】:
美好的一天。我正在尝试为我的 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) 而不是插入新行?为什么它包含 " 符号?
【问题讨论】:
标签: python python-3.x yaml pythonanywhere