Python Forum

Full Version: PyYAML read list of int
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using a config.yaml and want to read a list of integers in "week":
{'worksheet_name': 'March 2024',
 'week': [12, 13],
 'name': 'John Doe',
 'username': 'jd9003'}
Reading this yaml with
with open("config.yaml", encoding='utf8') as f:
    cfg = yaml.load(f, Loader=yaml.FullLoader)
strangely leads to this value
week: [[12, 13]]
and not to
week: [12, 13]
Which format should be used for a list of int in yaml?
If i run your code i don't get week: [[12, 13]].
import yaml

with open("config.yaml", encoding='utf8') as f:
    cfg = yaml.load(f, Loader=yaml.FullLoader)
Test.
>>> cfg
{'name': 'John Doe',
 'username': 'jd9003',
 'week': [12, 13],
 'worksheet_name': 'March 2024'}

>>> cfg['week']
[12, 13]
>>> cfg['week'][1]
13
>>> type(cfg['week'][1])
<class 'int'>
So get back Python data structure a dictionary contains a working list of integers.

file used: cofig.yaml
Output:
{'worksheet_name': 'March 2024', 'week': [12, 13], 'name': 'John Doe', 'username': 'jd9003'}
Quote:If i run your code i don't get week: [[12, 13]].

Error found and request for forgiveness attached - for some reason, the assignment looked like this (and is strangely executable without errors):
week = [cfg['week']]