根据某个键值(LEDbrightness)从.txt文件中删除一行 [英] remove a line from .txt file based on some key value(LEDbrightness)

查看:91
本文介绍了根据某个键值(LEDbrightness)从.txt文件中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i想要根据一些关键值(LEDbrightness)从.txt文件中删除一行。

例如:我将拥有LEDbrightness = 2 in。 txt文件。要么我需要将LEDbrightness的值从2更改为3,要么我需要删除整行LEDbrightness = 2并需要添加LEDbrightness = 3。我可以补充一点。但它增加了现有的。

LEDbrightness = 2

LEDbrightness = 3



i不能删除LEDbrightness = 2。请帮帮我。我真的陷入了这个。

解决方案

一个简单的方法是:

逐行读取输入文件并仅复制到输出必需/修改的。

最后删除输入文件并正确地重命名输出文件(对于小文件,您可以读取内存中的整个文件,执行修改然后将其写回磁盘)。

Hi,
i want to remove a line from .txt file based on some key value(LEDbrightness).
for eg: i will have LEDbrightness=2 in .txt file . Either i need to change the value of LEDbrightness from 2 to 3 or i need to remove the entire line LEDbrightness=2 and need to add LEDbrightness=3. I am able to add that. but it is adding to the existing one.
LEDbrightness=2
LEDbrightness=3

i am not able to remove LEDbrightness=2. Please help me on this. i m really stuck in this.

解决方案

A simple approach is:
Read the input file line by line and copy to the output only the required/modified ones.
At the end remove the input file and properly rename the output one (for small files you may read the whole file in memory, preform the modifications and then write back it to the disk).


这篇关于根据某个键值(LEDbrightness)从.txt文件中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆