【发布时间】:2021-08-30 05:37:25
【问题描述】:
我有这个简单的程序
import yaml
import sys
def main(argv):
#dddd
with open('file_base.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
#print(data)
#print(type(data))
#Do some transformations to data
#Then we save it to a file called the_new.yaml
with open('the_new.yaml', 'w') as f:
data2 = yaml.dump(data, f)
print(data2)
print(type(data2))
if __name__ == '__main__':
main(sys.argv[1:])
最终目标是对数据进行一些转换,但在这个问题中,我想问一些更基本的问题。在上面的脚本中,没有发生任何转换。
但是,在查看输出时,数据发生了变化。例如 file_base.yaml 有
first_data: # Required.
type: ad
class_names: ["hello",
"dog",
"cat",
"lion",
"human"]
但是 the_new.yaml 有
first_data:
class_names:
-"hello"
-"dog"
-"cat"
-"lion"
-"human"
type: ad
如您所见,存在两个问题:
- 田野乱了
- 数组从
[ one, two,three]变为使用“-”
我该如何纠正这个问题?
【问题讨论】: