11

I'm trying to make a script to back up a MySQL database. I have a config.yml file:

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:

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?

2
  • 2
    Welcome to SO btw. The question may be simple, the formatting is good and all useful elements are here. Enjoy your stay, and don't forget to mark an answer as approved when your question is fully answered :)
    – spectras
    Feb 1, 2017 at 8:44
  • 1
    You should not be using yaml.load() as it can be unsafe and from your question it looks like you don't have the experience to judge correctly if you are affected or not (use .safeload() instead). Your code also has a problem if reading config.yml throws an error (while parsing), in that case your file might not be closed properly. You should use the with statement
    – Anthon
    Feb 1, 2017 at 11:32

3 Answers 3

11

There are 2 issues:

  • 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.

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'
2

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.

Then, for Python's yaml parser.

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).

# 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 here you won't regret it)

In your case:

config['DB_NAME'] # moodle_data
3
  • “JSON compiler” does not really make sense as parsing a JSON document produces a data structure, not a program. I suppose you meant JSON parser, and more specifically, python's default json parser. Good point insisting on using the correct tool for db backup btw.
    – spectras
    Feb 1, 2017 at 8:36
  • 1
    Whoops... Thanks, fixed it!
    – math2001
    Feb 1, 2017 at 8:40
  • import yaml config=yaml.load(open('config.yml')) db=config['DB_NAME'] print db still TypeError comes up "string indices must be integers, not str" Feb 1, 2017 at 8:54
0

Try this:

import yaml
with open('config.yaml', 'r') as f:
    doc = yaml.load(f)

To access "DB_NAME" you can use:

txt = doc["DB_NAME"]
print txt
2
  • it says TypeError: string indices must be integers, not str Feb 1, 2017 at 8:24
  • Most likely do: doc[0]['DB_NAME'], cause reasons.
    – Kevin C
    Nov 13, 2019 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.