0

I need to replace some string by others. I'm using function pathlib to do that it's working fine but I have a problem when there is two same string in my file and I need to change just one

My file (wireless.temp) is like this example

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'

For example, I need to change string like 'disabled', 'hidden', 'ssid', 'key', in config station and/or config device. Right now I'm using this code

    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)

I have a problem with this code, first when I execute my output is

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'

option disabled line in config 'client' section, my program add another 0 all time also I want to lighten my code because I need to do that for many others string.

Does anyone have an idea?

Thanks

  • 1
    Can you clarify: the pathlib module is new in Python 3.4, and you have Python 2.7 in your title. What version of Python are you using? Are you using pathlib? What is path? Is it just a string, and you aren’t using pathlib at all? – AJNeufeld Apr 14 '19 at 22:42
  • 2
    Do not add comments to your own question; edit your question to clarify. As presently stated in your comments, path is a `Path()` object, but in the question, you call `path.replace()` with two arguments, and `Path.replace()` only allows 1 argument. Provide a clear [mcve] please. – AJNeufeld Apr 14 '19 at 23:05

1 Answers1

0

The Path and pathlib2 is a red-herring. You are using that to find and read a file into a string. The issue is replacing text in a narrow section of the whole text. Specifically, between 'config device' and the next 'config ...' item

You can use .find() to find the beginning of the correct config section, and again to find the start of the next config section. Ensure you treat -1, for not found, as the end of the section. The text within that range can be modified, and then combine the resulting modification with the unmodified parts that came before and after it.

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)

Outputs:

config device
   .....
   option key 'qwerty'
   .....
config station
   .....
   option key 's3cr3t'
   .....
config otherthing
   .....
   option key 'qwerty'
   .....
AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
  • Before I used this method but I have many parameters in many files so I started to use pathlib2 to lighten my code. I think I need to create a function if I don't a huge code. Thanks @AJNeufeld – Black_Syphilis Apr 15 '19 at 02:10
  • If the above is not helpful, because you have “many parameters in many files”, it is because your question states “I need to change just the key string in the config station”. You’ve never shown us your code which tries to do this for many files, for many parameters. Where do you get the many file names, parameters, actualWifiPass and newWifiPass values from? – AJNeufeld Apr 15 '19 at 14:22
  • Yes my bad, but your method is useful, I'll edit my post. – Black_Syphilis Apr 17 '19 at 19:20