使用 Python 读取 YAML 文件导致 AttributeError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41974628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading YAML file with Python results in AttributeError
提问by Yerlan Yeszhanov
I'm trying to make a script to back up a MySQL database. I have a config.yml
file:
我正在尝试制作一个脚本来备份 MySQL 数据库。我有一个config.yml
文件:
DB_HOST :'localhost'
DB_USER : 'root'
DB_USER_PASSWORD:'P@$$w0rd'
DB_NAME : 'moodle_data'
BACKUP_PATH : '/var/lib/mysql/moodle_data'
Now I need to read this file. My Python code so far:
现在我需要阅读这个文件。到目前为止,我的 Python 代码:
import yaml
config = yaml.load(open('config.yml'))
print(config.DB_NAME)
And this is an error that comes up:
这是一个出现的错误:
file "conf.py", line 4, in <module>
print(config.DB_NAME)
AttributeError: 'str' object has no attribute 'DB_NAME'
Does anyone have an idea where I made a mistake?
有谁知道我在哪里犯了错误?
采纳答案by Roel Schroeven
There are 2 issues:
有2个问题:
- As others have said, yaml.load() loads associative arrays as mappings, so you need to use
config['DB_NAME']
. - The syntax in your config file is not correct: in YAML, keys are separated from values by a colon+space.
- 正如其他人所说, yaml.load() 加载关联数组作为映射,因此您需要使用
config['DB_NAME']
. - 您的配置文件中的语法不正确:在 YAML 中,键与值之间用冒号+空格分隔。
Should work if the file is formatted like this:
如果文件格式如下,应该可以工作:
DB_HOST: 'localhost'
DB_USER: 'root'
DB_USER_PASSWORD: 'P@$$w0rd'
DB_NAME: 'moodle_data'
BACKUP_PATH: '/var/lib/mysql/moodle_data'
回答by math2001
To backup your data base, you should be able to export it as a .sql
file. If you're using a specific interface, look for Export
.
要备份您的数据库,您应该能够将其导出为.sql
文件。如果您使用的是特定接口,请查找Export
.
Then, for Python's yaml parser.
然后,对于 Python 的 yaml 解析器。
DB_HOST :'localhost'
DB_USER : 'root'
DB_USER_PASSWORD:'P@$$w0rd'
DB_NAME : 'moodle_data'
BACKUP_PATH : '/var/lib/mysql/moodle_data'
is a key-value
thing (sorry, didn't find a better word for that one). In certain langage (such as PHP I think), they are converted to objects. In python though, they are converted to dicts(yaml parser does it, JSON parser too).
是一key-value
件事(对不起,没有找到更好的词来形容那个)。在某些语言中(例如我认为的 PHP),它们被转换为objects。但是在 python 中,它们被转换为dicts(yaml 解析器可以,JSON 解析器也可以)。
# access an object's attribute
my_obj.attribute = 'something cool'
my_obj.attribute # something cool
del my_obj.attribute
my_obj.attribute # error
# access a dict's key's value
my_dict = {}
my_dict['hello'] = 'world!'
my_dict['hello'] # world!
del my_dict['hello']
my_dict['hello'] # error
So, that's a really quick presentation of dicts, but that should you get you going (run help(dict)
, and/or have a look hereyou won't regret it)
所以,这是一个非常快速的 dicts 介绍,但是如果你让你开始(运行help(dict)
,和/或看看这里你不会后悔)
In your case:
在你的情况下:
config['DB_NAME'] # moodle_data
回答by Shivkumar kondi
Try this:
尝试这个:
import yaml
with open('config.yaml', 'r') as f:
doc = yaml.load(f)
To access "DB_NAME" you can use:
要访问“DB_NAME”,您可以使用:
txt = doc["DB_NAME"]
print txt