how find a word in txt file

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
mehdi_ziyaei
Posts: 16
Joined: Tue May 21, 2019 5:58 pm

how find a word in txt file

Post by mehdi_ziyaei » Fri May 24, 2019 12:55 am

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 ?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how find a word in txt file

Post by jimmo » Fri May 24, 2019 1:57 am

Does your board have `ujson` (I'm pretty sure it will)

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'}
(Sorry I didn't answer your actual question, but doing this with regular expressions is way more complicated)

mehdi_ziyaei
Posts: 16
Joined: Tue May 21, 2019 5:58 pm

Re: how find a word in txt file

Post by mehdi_ziyaei » Fri May 24, 2019 9:19 am

My essid and password are always changing. So I think I should use the re library

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how find a word in txt file

Post by jimmo » Fri May 24, 2019 10:31 am

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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: how find a word in txt file

Post by dhylands » Fri May 24, 2019 1:56 pm

I normally just use a python file called something like config.py which contains

Code: Select all

SSID = "myssid"
password = "mypassword"
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.

Post Reply