hi all .i want to find essid and password in my txt file and change my wifi config.
-------------------------------------------------------------------------------------------------
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
----------------------------------------------------------------------------------------------------
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 ?
how find a word in txt file
Re: how find a word in txt file
Does your board have `ujson` (I'm pretty sure it will)
(Sorry I didn't answer your actual question, but doing this with regular expressions is way more complicated)
Code: Select all
import ujson
config = {'essid': 'hello', 'password': 'secret'}
ujson.dump(config, open('config.txt', 'w'))
Code: Select all
>>> ujson.load(open('config.txt', 'r'))
{'password': 'secret', 'essid': 'hello'}
-
- Posts: 16
- Joined: Tue May 21, 2019 5:58 pm
Re: how find a word in txt file
My essid and password are always changing. So I think I should use the re library
Re: how find a word in txt file
That doesn't matter - the file is still a text file that you can edit.
It'll be a different format to your current one but still just as easy to edit.
It'll be a different format to your current one but still just as easy to edit.
Re: how find a word in txt file
I normally just use a python file called something like config.py which contains and then you can just import config and use config.SSID and config.password
If you want to store new passwords programmatically, just write out a new file.
Code: Select all
SSID = "myssid"
password = "mypassword"
If you want to store new passwords programmatically, just write out a new file.