简体   繁体   English

如何更改YAML文件中的行?

[英]How to change a line in a YAML file?

I have a bunch of YAML files and each file has one line of code that needs to be changed. 我有一堆YAML文件,每个文件都有一行需要更改的代码。 I am trying to automate it using Python, what's the best way to do this? 我正在尝试使用Python自动化它,最好的方法是什么?

Right now I have a list of the files, and I plan on opening each one and finding the line needed to be changed then replacing it. 现在,我有了文件列表,我计划打开每个文件并找到需要更改的行,然后替换它。

Is this possible? 这可能吗? I can't seem to figure out how to replace the line. 我似乎不知道如何替换该行。 I know the exact line number, could that help? 我知道确切的行号,能帮上忙吗?

Since you know the exact line number, this is quite easy - it doesn't even matter that the file is YAML if you know exactly what you need to replace it with. 因为您知道确切的行号,所以这很容易-如果您确切知道需要用什么替换它,则文件是否为YAML都没有关系。

I'm assuming here that all the files that need their lines changed are in the same directory, with no other YAML files. 我在这里假设所有需要更改其行的文件都在同一目录中,没有其他YAML文件。 If this isn't the case then the program will require fine-tuning of course. 如果不是这种情况,则当然需要对程序进行微调。

import os
line_number = 47  # Whatever the line number you're trying to replace is
replacement_line = "Whatever string you're replacing this line with"

items = os.listdir(".")  # Gets all the files & directories in the folder containing the script

for file_name in items:  # For each of these files and directories,
    if file_name.lower().endswith(".yaml"):  # check if the file is a YAML. If it is:
        with open(file_name, "w") as file:  # Safely open the file
            data = file.read()  # Read its contents
            data[line_number] = replacement_line  # Replace the line
            file.write(data)  # And save the file

Note that if your files are .yml rather than .yaml, then you would have to change that in the code. 请注意,如果文件是.yml而不是.yaml,则必须在代码中进行更改。 Additionally, if your files are too large this could cause problems as each file is loaded into memory. 此外,如果您的文件太大,则可能会导致问题,因为每个文件都已加载到内存中。

If this doesn't work for you, then there are other solutions around the internet, including Stack Overflow! 如果这对您不起作用,那么互联网上还有其他解决方案,包括Stack Overflow!

I use the pyyaml library ( pip install pyyaml ): 我使用pyyaml库( pip install pyyaml ):

It handles the full YAML syntax as shown in the example below: 它处理完整的YAML语法,如下例所示:

  • python example
import yaml

with open("data.yaml", "w+") as FILE:
    data = yaml.load(FILE)
    data.pop("token_default")
    yaml.dump(data)


  • data.yaml
token_default: &token
  token      : !!str
  type       : # var, field, list, vector
  version    : 0.1
  namespace  : cam_tokens
  properties :
    unit   : !!seq [0, 0, 0, 0, 0, 0]
    min    : !!float +inf
    max    : !!float -inf
    argPos : !!int -1
  required   :
    - unit
    - argPos

mass:
  <<: *token
  token: mass
  namespace  : cam_tokens
  properties :
    unit   : !!seq [1, 0, 0, 0, 0, 0]
  required   :
    - unit
    - argPos

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

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