#๐Ÿ”’ Why does this print 'None'?

18 messages ยท Page 1 of 1 (latest)

uneven kernel
#

i'm trying to use pyyaml to parse a YML file, but it returns none

import yaml
file = open('items.yml', 'r')
print(file.read())
item_database = yaml.safe_load(file)
print(item_database)

and the YML:

materials:
    iron:
        difficulty: 1.5
        melting_point: 1300
        molten_variant: 'molten'
        boiling_point: 2800
        vapour_variant: 'vapour'
        corrosive: false
        radioactive: false
        half_life: -1
        variants:
            ore:
                replaces: 'iron_ore'
                tags:
                    - 'is_raw'
                    - 'is_ore'
                    - 'is_solid'
                    - 'is_metal'
            ingot:
                replaces: 'iron_ingot'
                tags:
                    - 'is_forged'
                    - 'is_solid'
                    - 'is_metal'
                    
            molten:
                replaces: 'lava_bucket'
                tags:
                    - 'is_liquid'
                    - 'is_metal'
            
            plate:
                replaces: 'bowl'
                tags:
                    - 'is_liquid'
                    - 'is_metal'
                    - 'is_plate'
ionic pineBOT
#

@uneven kernel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

uneven kernel
#

i expect the code to return a dict inteh form that the YML takes

bold flame
#

your file.read has "exhausted" the file.

uneven kernel
#

'exhausted'?

bold flame
#

ya watch

#

comment out the print

#
import yaml

file = open("/tmp/items.yml")
# print(file.read())
item_database = yaml.safe_load(file)
print(item_database)
#

now we see ```py

{'materials': {'iron': {'difficulty': 1.5, 'melting_point': 1300, 'molten_variant': 'molten', 'boiling_point': 2800, 'vapour_variant': 'vapour', 'corrosive': False, 'radioactive': False, 'half_life': -1, 'variants': {'ore': {'replaces': 'iron_ore', 'tags': ['is_raw', 'is_ore', 'is_solid', 'is_metal']}, 'ingot': {'replaces': 'iron_ingot', 'tags': ['is_forged', 'is_solid', 'is_metal']}, 'molten': {'replaces': 'lava_bucket', 'tags': ['is_liquid', 'is_metal']}, 'plate': {'replaces': 'bowl', 'tags': ['is_liquid', 'is_metal', 'is_plate']}}}}}

uneven kernel
#

i just have and it works, why is this the case?

bold flame
#

an open file is like an old-school reel-to-reel tape player!
Reading the file plays the tape

#

you have to rewind it to use it again

uneven kernel
#

k fair

#

ill keep that in mind

#

thanks

#

!close

ionic pineBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.