简体   繁体   English

我可以使用 Python configparser 删除 config.ini 文件的一部分中的特定值吗?

[英]Can I delete a specific value within a section of a config.ini file using Python configparser?

I have a config.ini file and I am trying to delete a specific value in a section of my config.ini file.我有一个 config.ini 文件,我正在尝试删除 config.ini 文件部分中的特定值。 I basically want to delete an index within a list.我基本上想删除列表中的索引。 The config.ini file is: config.ini文件是:

[INPUT]
intervals = [[4000, 6000], [25000, 55000]]

For example, I want to delete [4000, 6000] , so that the new config.ini file becomes:比如我想删除[4000, 6000] ,这样新的config.ini文件就变成了:

[INPUT]
intervals = [[25000, 55000]]

My code is below with two functions, one to import the contents of config.ini ( load_args() ) and another to delete a section of config.ini ( delete_args() ).我的代码在下面有两个函数,一个是导入 config.ini 的内容( load_args() ),另一个是删除 config.ini 的一部分( delete_args() )。 The delete function is based off an answer to How to remove a section from an ini file using Python ConfigParser?删除 function 是基于如何使用 Python ConfigParser?

import configparser
import ast

def load_args():
    config = configparser.ConfigParser()
    config.read('config.ini')
    interval = ast.literal_eval(config['INPUT']['intervals'])
    print(interval)

load_args()

def delete_args():
    config = configparser.ConfigParser()
    with open('config.ini', 'r+') as s:
        config.readfp(s)  
        config.remove_section('INPUT')
        s.seek(0)
        config.write(s)
        s.truncate()
        interval = ast.literal_eval(config['INPUT']['intervals'])
        print(interval)

delete_args()

The current delete_args() function above deletes everything in the config.ini.上面的当前delete_args() function 删除了 config.ini 中的所有内容。 I understand that delete_args() needs a way to find the value [4000, 6000] in config.ini , so I was wondering if is possible to pass in [4000, 6000] as an argument into delete_args() that then finds the section [INPUT] , and finds the specified interval ( [4000, 6000] ) within intervals and deletes it.我知道delete_args()需要一种在config.ini中找到值[4000, 6000]的方法,所以我想知道是否可以将[4000, 6000]作为参数传递给delete_args()然后找到该部分[INPUT] ,并在区间内查找指定intervals[4000, 6000] )并将其删除。

If I understand you correctly you want to remove a specific interval from the multi-dimensional list by argument in your delete_args function when you update the config.ini.如果我理解正确,您希望在更新 config.ini 时通过delete_args function 中的参数从多维列表中删除特定间隔。 I came up with this solution:我想出了这个解决方案:

import configparser 
import ast

parser = configparser.ConfigParser()
interval = []

def load_args():
    global interval
    parser.read('config.ini')
    interval = ast.literal_eval(parser['INPUT']['intervals'])
    print(interval)

load_args()

def delete_args(index):
    global interval
    interval.pop(index)
    parser.set('INPUT', 'intervals', str(interval))
    with open("config.ini", "w+") as configfile:
        parser.write(configfile)

def delete_args2(ele):
    global interval
    interval = [x for x in interval if x != ele]
    parser.set('INPUT', 'intervals', str(interval))
    with open("config.ini", "w+") as configfile:
        parser.write(configfile)

#delete_args(0)
delete_args2([4000, 6000])

delete_args(0)

load_args()

Output Output

[[4000, 6000], [25000, 55000]]
[[25000, 55000]]

I used a bit different update strategy, and just pop() the element at the specifid index.我使用了一些不同的更新策略,只是pop()指定索引处的元素。 I think this is a very clean way.我认为这是一种非常干净的方式。 If you prefer to pass in a value to remove you can use interval.remove(ele) or something more robust like [x for x in interval if x != ele] instead of the pop(index) .如果您更喜欢传入一个值来删除,您可以使用interval.remove(ele)或更强大的东西,例如[x for x in interval if x != ele]而不是pop(index) Note: I've defined interval globally for the sake of simplicity but that's not important.注意:为了简单起见,我已经在全局范围内定义了interval ,但这并不重要。

This is a little bit different approach, but still might work for your purpose.这是一种有点不同的方法,但仍然可能适用于您的目的。 Instead of deleting the intervals we may update it as follows:我们可以按如下方式更新它,而不是删除intervals

import configparser


def update_args(section, element, value):
    config = configparser.ConfigParser()
    config.read('config.ini')
    config.set(section, element, value)
    with open('config.ini', 'w') as f:
        config.write(f)


update_args("INPUT", "intervals", "[25000, 55000]")

By the way I have just heard about ini files here in this question and try to solve this problem.顺便说一句,我刚刚在这个问题中听说过ini文件,并尝试解决这个问题。 So, I am hoping that I did not something completely unrelated to the question.所以,我希望我没有做与这个问题完全无关的事情。

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

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