和我的setting.text
“ \ n -----------时间----------- \ nt =(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 -----------结束----------- \ n“
-----------时间-----------
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
-----------结束-----------
我想找到w_e = **********。只有星号(任何wifi ESSID)没有w_e =。如何找到? 我的代码不起作用。如何解决?
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
答案 0 :(得分:0)
不使用正则表达式-遍历文件;找到以'w_e='
开头的行;用切片从该行提取essid。
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
使用r'w_e=(.*)$'
作为正则表达式模式:
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!')