Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyYAML read list of int
#1
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?
Reply
#2
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'}
zisco likes this post
Reply
#3
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']]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 490 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,653 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  How to call/read function for all elements in my list in python johnny_sav1992 1 2,102 Jul-27-2020, 04:19 PM
Last Post: buran
  How to read a text file into a list or an array musouka 2 2,908 Oct-07-2019, 01:54 PM
Last Post: musouka
  Question about YAMLLoadWarning (PyYAML error / deprecated) UniKlixX 4 4,273 Sep-10-2019, 11:49 AM
Last Post: UniKlixX
  PyYAML millpond 2 2,955 Aug-01-2019, 03:19 AM
Last Post: millpond
  ThreadPoolExecutor read file to list DaLiPy 3 6,279 Jun-11-2019, 05:55 AM
Last Post: DaLiPy
  Read list of IP addresses from file and return a network dflick 4 4,920 Oct-27-2018, 09:33 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020