简体   繁体   English

将数据转储到包含 yaml 代码的现有 yaml 文件中

[英]Dumping data to existing yaml file with yaml code inside

I'm dumping some data from an .csv file to .yml file using yaml.dump.我正在使用 yaml.dump 将一些数据从 .csv 文件转储到 .yml 文件。 My problem is that when i dump data to the .yml file.我的问题是当我将数据转储到 .yml 文件时。 All the current yaml iside of it vanishes and only the new yaml from the .csv file shows.它的所有当前 yaml 都消失了,只有 .csv 文件中的新 yaml 显示。

Examples:例子:

Before I dump yaml to .yml file:在我将 yaml 转储到 .yml 文件之前:

Text: 'This is some yaml'
MoreText: 'This is more yaml'

After I dump yaml (All the current yaml vanihes and only the new shows):在我转储 yaml 之后(所有当前的 yaml 都消失了,只有新节目):

NewYamlFromCsvFile: 'This is new yaml'

What I want:我想要的是:

Text: 'This is some yaml'
MoreText: 'This is more yaml'
NewYamlFromCsvFile: 'This is new yaml'

Here's my yaml.dump code:这是我的 yaml.dump 代码:

            yaml.dump(
            df.loc[(df['NAME'] == pack)].to_dict(orient='records'), # Send keywords to the right pack, etc java keywords to java pack.
            outfile,
            sort_keys=False,
            width=72, 
            indent=4
        )

I there any way to not delete the current yaml when new yaml is dumped?当新的 yaml 被转储时,我有什么方法可以不删除当前的 yaml? Thanks!谢谢!

You should open the output file in 'a' mode (append mode):您应该以“a”模式(附加模式)打开输出文件:

with open('output.yml', 'a') as outfile:
       yaml.dump(your_data, outfile, other_args )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM