-2

i'm use micropython and i want to find essid and password in my txt file and change my wifi config.

and my setting.text

"\n -----------Time----------- \n t=(2019,5,23,4,40,00,4,143) \n -----------Wifi----------- \n w_e=any-wifi-name \n w_p=any-wifi-password \n ----------Wifi_new---------- \n w_n_e=wifi_name \n w_n_p=wifi_password \n ---------Nodemcu_wifi--------- \n n_e=KENOK KENOK \n n_p=123456789000 \n -----------End----------- \n"


-----------Time-----------

t=(2019,5,23,4,40,00,4,143)

-----------Wifi-----------

w_e=any-wifi-nam

w_p=any-wifi-password

-----------Wifi_new-----------

w_n_e=wifi_name

w_n_p=wifi_password

-----------Nodemcu_wifi-----------

n_e=KENOK KENOK

n_p=123456789000

-----------End-----------

i want to find w_e=********** . only star(any wifi essid) Without w_e= .how find? my code not work .how fix it?


def wifi_connect():
file = open("setting.text" , "r")
wifi_essid =re.sub(r'[w_e=]+.+\n$'," ",file.read())
print(wifi_essid)
...
...
...
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True) 
sta_if.connect(wifi_essid,wifi_password)
return

1 Answers1

0

Without using a regular expression - iterate over the file; find the line that starts with 'w_e='; extract the essid from that line with a slice.

with open("setting.text" , "r") as f:
    for line in f:
        if line.startswith('w_e='):
            line = line.strip()
            essid = line[4:]
            print(essid)
            break

Using r'w_e=(.*)$' for the regex pattern:

pattern = r'w_e=(.*)$'

with open("setting.text" , "r") as f:
    text = f.read()
m = re.search(pattern, text, flags=re.M)
if m:
    print(m.group(1))
else:
    print('no match!')
wwii
  • 23,232
  • 7
  • 37
  • 77