pyyaml yaml.load returns str 而不是 dict
pyyaml yaml.load returns str instead of dict
使用 pyyaml 和 python3,以下内存中的 yaml 字符串加载没有错误,但是 yaml.full_load() returns 是一个 str 而不是一个 dict。这是预期的行为吗?谢谢。
main:
orgs:
&org1 org1:
categories:
- one
- two
- three
&org2 org2:
categories:
- one
- two
people:
- userid: user1
org: *org1
roles:
- roleA
- roleB
- userid: user2
org: *org2
roles:
- roleA
print("MAIN_YAML = " + os.getenv("MAIN_YAML"))
try:
MAIN_YAML = yaml.full_load(os.getenv("MAIN_YAML"))
print("PARSED SUCCESSFULLY")
print(isinstance(MAIN_YAML, dict))
print(type(MAIN_YAML))
except (yaml.YAMLError, yaml.YAMLLoadWarning) as e:
print(e)
MAIN_YAML = main:orgs:&org1org1:categories:-one-two-three&org2org2:categories:-one-twopeople:-userid:user1org:*org1roles:-roleA-roleB-userid:user2org:*org2roles:-roleA
PARSED SUCCESSFULLY
False
<class 'str'>
这是创建单行代码的 shell 脚本:
tr -d '\n\t' < main.yaml > temp.yaml
tr -d ' ' < temp.yaml > main_squeezed.yaml
MAIN_YAML=$(cat main_squeezed.yaml)
看来您是从没有 YAML 文件的环境变量中加载它的"as is"(带有换行符)。
当字符串包含换行符时有效:
>>> s = """main:
... orgs:
... &org1 org1:
... categories:
... - one
... - two
... - three
... &org2 org2:
... categories:
... - one
... - two
... people:
... - userid: user1
... org: *org1
... roles:
... - roleA
... - roleB
... - userid: user2
... org: *org2
... roles:
... - roleA"""
>>>
>>> import yaml
>>> yaml.full_load(s)
{'main': {'orgs': {'org1': {'categories': ['one', 'two', 'three']}, 'org2': {'categories': ['one', 'two']}}, 'people': [{'userid': 'user1', 'org': 'org1', 'roles': ['roleA', 'roleB']}, {'userid': 'user2', 'org': 'org2', 'roles': ['roleA']}]}}
当字符串是一行文本时不起作用:
>>> t = s.replace('\n', '').replace(' ', '') # same thing, but one line
>>> yaml.full_load(t)
'main:orgs:&org1org1:categories:-one-two-three&org2org2:categories:-one-twopeople:-userid:user1org:*org1roles:-roleA-roleB-userid:user2org:*org2roles:-roleA'
使用 pyyaml 和 python3,以下内存中的 yaml 字符串加载没有错误,但是 yaml.full_load() returns 是一个 str 而不是一个 dict。这是预期的行为吗?谢谢。
main:
orgs:
&org1 org1:
categories:
- one
- two
- three
&org2 org2:
categories:
- one
- two
people:
- userid: user1
org: *org1
roles:
- roleA
- roleB
- userid: user2
org: *org2
roles:
- roleA
print("MAIN_YAML = " + os.getenv("MAIN_YAML"))
try:
MAIN_YAML = yaml.full_load(os.getenv("MAIN_YAML"))
print("PARSED SUCCESSFULLY")
print(isinstance(MAIN_YAML, dict))
print(type(MAIN_YAML))
except (yaml.YAMLError, yaml.YAMLLoadWarning) as e:
print(e)
MAIN_YAML = main:orgs:&org1org1:categories:-one-two-three&org2org2:categories:-one-twopeople:-userid:user1org:*org1roles:-roleA-roleB-userid:user2org:*org2roles:-roleA
PARSED SUCCESSFULLY
False
<class 'str'>
这是创建单行代码的 shell 脚本:
tr -d '\n\t' < main.yaml > temp.yaml
tr -d ' ' < temp.yaml > main_squeezed.yaml
MAIN_YAML=$(cat main_squeezed.yaml)
看来您是从没有 YAML 文件的环境变量中加载它的"as is"(带有换行符)。
当字符串包含换行符时有效:
>>> s = """main:
... orgs:
... &org1 org1:
... categories:
... - one
... - two
... - three
... &org2 org2:
... categories:
... - one
... - two
... people:
... - userid: user1
... org: *org1
... roles:
... - roleA
... - roleB
... - userid: user2
... org: *org2
... roles:
... - roleA"""
>>>
>>> import yaml
>>> yaml.full_load(s)
{'main': {'orgs': {'org1': {'categories': ['one', 'two', 'three']}, 'org2': {'categories': ['one', 'two']}}, 'people': [{'userid': 'user1', 'org': 'org1', 'roles': ['roleA', 'roleB']}, {'userid': 'user2', 'org': 'org2', 'roles': ['roleA']}]}}
当字符串是一行文本时不起作用:
>>> t = s.replace('\n', '').replace(' ', '') # same thing, but one line
>>> yaml.full_load(t)
'main:orgs:&org1org1:categories:-one-two-three&org2org2:categories:-one-twopeople:-userid:user1org:*org1roles:-roleA-roleB-userid:user2org:*org2roles:-roleA'