简体   繁体   English

如何使用 Python 将一组文本插入文件的特定行号

[英]How to insert a set of text to a particular line number of a file using Python

I am trying to write a python program that can input a set of text in dictionary format and also a line number.我正在尝试编写一个 python 程序,它可以输入一组字典格式的文本以及一个行号。 I need to add this set of text to a specific line number of a .yaml file.我需要将这组文本添加到 .yaml 文件的特定行号。

I have a Kubernetes deployment.yaml file and I need my Python program to add the text from the dictionary to a particular line number (line 29, in between cpu: "500m" and volumeMounts: ) in the deployment.yaml file.我有一个 Kubernetes deployment.yaml 文件,我需要我的 Python 程序将字典中的文本添加到 deployment.yaml 文件中的特定行号(第 29 行,在 cpu: "500m"volumeMounts:之间)。

deployment.yaml部署.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
      volumeMounts:
      - name: ephemeral
        mountPath: "/tmp"
  volumes:
    - name: ephemeral
      emptyDir: {}

test.py测试.py

yaml_change={
  "change": '''
      tolerations:
      - key: "example-key"
        operator: "Exists"
        effect: "NoSchedule"
  '''
}

line_number = 29
yaml_modification = yaml_change['change']

with open('values.yaml', 'r') as FileRead:
    yaml_data = FileRead.readlines()
    yaml_data = yaml_data[line_number-2]+yaml_modification
    print(yaml_data)

with open('values.yaml', 'w') as FileWrite:
    FileWrite.writelines(yaml_data)

When I run the python file, the text file in the dictionary gets added to the .yaml file.当我运行 python 文件时,字典中的文本文件被添加到 .yaml 文件中。 However, all the other contents are lost.但是,所有其他内容都丢失了。

Before在此处输入图像描述

After在此处输入图像描述

Does anyone know how to get this requirement done?有谁知道如何完成这个要求?

Try changing the line yaml_data = yaml_data[line_number-2]+yaml_modification to尝试将yaml_data = yaml_data[line_number-2]+yaml_modification行更改为

yaml_data.insert(line_number - 1, yaml_modification)

yaml_data is a list of all lines in the file. yaml_data是文件中所有行的列表。 The list.insert(idx, obj) function inserts the object at a given position. list.insert(idx, obj)函数在给定位置插入对象。

Your solution doesn't work because you are adding the content you want to add and the line before ( yaml_data[line_number-2] ).您的解决方案不起作用,因为您正在添加要添加的内容和之前的行( yaml_data[line_number-2] )。

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

相关问题 如何使用python从文本文件中提取特定行 - How to extract particular line from text file using python 用python替换文本文件中的特定行? - replace particular line in text file using python? 如何使用python中的行号从文本文件中删除一行 - How to delete a line from a text file using the line number in python Python 如何替换文本文件中特定行中的特定单词? - Python How to replace a particular word in a particular line in a text file? 更改特定行中的特定值(例如行号:57),并使用python保存具有相同文件名的文件 - Changing a particular value in a particular line (say line number:57) and saving the file with same file name using python 如何使用 Python 删除文本文件中的某些行号集? - How to remove certain set of line numbers in a text file using Python? 如何在Python中的单独变量中存储文本文件特定行的值 - How to store values of a particular line of a text file in separate variables in Python 如何从 python 中的文本文件的特定行打印 - how to print from a particular line from text file in python 如何使用 python 从记事本中提取或解析特定的文本行? - How to extract or parse particular line of text from notepad using python? 如何根据条件使用python删除xml文件中的特定行? - How to delete a particular line in xml file based on the condition using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM