【问题标题】:Python 2.7 path replace stringPython 2.7 路径替换字符串
【发布时间】:2019-09-04 22:10:09
【问题描述】:

我需要用其他字符串替换一些字符串。我正在使用函数 pathlib 来做到这一点,它工作正常,但是当我的文件中有两个相同的字符串时我遇到了问题,我只需要更改一个

我的文件(wireless.temp)就是这个例子

config 'conf'
    option disabled '0'
    option hidden '1'
    option ssid 'confSSID'
    option encryption 'psk2'
    option key 'qwerty'

config 'client'
    option disabled '0'
    option hidden '0'
    option ssid 'clientSSID'
    option encryption 'psk2'
    option key 'qwerty'

例如,我需要在配置站和/或配置设备中更改“禁用”、“隐藏”、“ssid”、“密钥”等字符串。现在我正在使用这个代码

    f1=open('wireless.temp', 'r').read()
    f2=open('wireless.temp','w')

    #checkbox from QT interface
    if self.chkWifiEnable.isChecked():
        newWifiEnable = "0"
    else:
        newWifiEnable = "1"

    start = f1.find("config 'client'")
    print start
    end = f1.find("config", start + 1)
    print end
    if end < 0:
        end = len(f1)
    station = f1[start:end]
    print station
    print f1.find("disabled")
    print f1.find("'")
    actualValue = f1[(station.find("disabled")+10):station.find("'")]
    print actualValue
    station = station.replace("disabled '" + actualValue, "disabled '" + newWifiEnable)
    print station
    m = f1[:start] + station + f1[end:]
    f2.write(m)

我对这段代码有问题,首先当我执行时我的输出是

config 'conf'
    option device 'radio0'
    option ifname 'conf'
    option network 'conf'
    option mode 'ap'
    option disabled '0'
    option hidden '1'
    option isolate '1'
    option ssid 'Conf-2640'
    option encryption 'psk2'
    option key '12345678'

config 'client'
    option device 'radio0'
    option ifname 'ra0'
    option network 'lan'
    option mode 'ap'
    option disabled '00'    <---- problem
    option hidden '0'
    option ssid 'FW-2640'
    option encryption 'psk2'
    option key '12345678'

config 'client' 部分中的 option disabled 行,我的程序一直添加另一个 0 我也想减轻我的代码,因为我需要为许多其他字符串这样做。

有人有想法吗?

谢谢

【问题讨论】:

  • 您能否澄清一下:pathlib 模块是 Python 3.4 中的新模块,并且您的标题中有 Python 2.7。你使用的是什么版本的 Python?你在使用路径库吗?什么是路径?它只是一个字符串,你根本没有使用 pathlib 吗?
  • 不要将 cmets 添加到您自己的问题中;编辑您的问题以澄清。正如目前在您的 cmets 中所述,路径是一个 Path() 对象,但在问题中,您使用两个参数调用 path.replace(),而 Path.replace() 只允许一个参数。请提供明确的minimal reproducible example

标签: python string python-2.7 replace pathlib


【解决方案1】:

Pathpathlib2 是一个红鲱鱼。您正在使用它来查找文件并将其读入字符串。问题是在整个文本的狭窄部分替换文本。具体来说,在'config device' 和下一个'config ...' 项目之间

您可以使用.find() 查找正确配置部分的开头,然后再次查找下一个配置部分的开头。确保将-1(未找到)视为本节的结尾。可以修改该范围内的文本,然后将生成的修改与之前和之后的未修改部分结合起来。

wirelessF = """
config device
   .....
   option key 'qwerty'
   .....
config station
   .....
   option key 'qwerty'
   .....
config otherthing
   .....
   option key 'qwerty'
   .....
"""

actualWifiPass = 'qwerty'
newWifiPass = 's3cr3t'

start = wirelessF.find("config station")
end = wirelessF.find("config ", start+1)
if end < 0:
   end = len(wirelessF)
station = wirelessF[start:end]
station = station.replace("key '"+actualWifiPass, "key '"+newWifiPass)
wirelessF = wirelessF[:start] + station + wirelessF[end:]

print(wirelessF)

输出:

config device
   .....
   option key 'qwerty'
   .....
config station
   .....
   option key 's3cr3t'
   .....
config otherthing
   .....
   option key 'qwerty'
   .....

【讨论】:

  • 在我使用这种方法之前,但我在很多文件中有很多参数,所以我开始使用 pathlib2 来减轻我的代码。如果我没有大量代码,我想我需要创建一个函数。谢谢@AJNeufeld
  • 如果以上没有帮助,因为您有“许多文件中的许多参数”,那是因为您的问题是“我需要更改配置站中的密钥字符串”。你从来没有向我们展示你的代码,它试图对许多文件、许多参数执行此操作。您从哪里获得许多文件名、参数、actualWifiPass 和 newWifiPass 值?
  • 是的,我的错,但你的方法很有用,我会编辑我的帖子。
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 2015-05-28
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2015-06-12
  • 1970-01-01
相关资源
最近更新 更多