B
B
BrenLike2020-07-30 20:09:29
Python
BrenLike, 2020-07-30 20:09:29

PyYaml - Can't load string from yaml, what should I do?

I want to import one prefix line from my config.yaml file but I can't because of a strange error

Console:
test.py:3: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
config = yaml.load("config.yaml")
Traceback (most recent call last):
File "test.py", line 5, in
print(config['prefix'])
TypeError: string indices must be

integers

import yaml

config = yaml.load("config.yaml")

print(config['prefix'])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-07-30
@SoreMix

The load function takes a string as an argument. You are passing to load not the text from the file, but the string " config.yaml "
Accordingly,

with open('config.yaml', 'r') as f:
    yaml.safe_load(f)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question