[英]Unable to parse yaml file into python
我正在嘗試將簡單 YAML 文件中的數據解析為 python 但是運行程序會產生錯誤。
#!/usr/bin/env python3
進口 yaml
以 open ('xyz.yml', 'r') 作為句柄:
try:
print("File opened")
data = yaml.full_load(handle)
print("data loaded")
print(data)
except yaml.YAMLError as error:
print(error)
YAML 文件包含以下內容。
名稱:nick #YAML 允許評論事物:-chair -table
但是,運行程序
➜ PYTHON python3 file.py 在“xyz.yml”第9行第9列中不允許打開文件映射值
語法或代碼有什么問題嗎? 相同的數據適用於 JSON。 I have tried the same code with JSON Data, of course with JSON structure data and loading the JSON library and necessary JSON calls in the python program
這里的問題是您的 YAML 文件,我相信它應該是:
name: nick # YAML allows comments
things:
- chair
- table
- sofa:
color: gray
age: 2
YAML 很大程度上取決於縮進,所以請記住這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.