python 正則比對漢字、簡單讀寫、打開txt檔案
win10環境,假設:
py腳本所在的目前目錄下有index.html檔案,現在,
要将index.html 中的漢字提取出來,儲存為目前目錄下的temp.txt,然後用notepad.txt打開檢視。代碼:
#coding=utf8
import os,re
with open(\'index.html\', mode=\'r\', encoding=\'UTF-8\') as f: # 打開檔案
data = f.read() # 讀取檔案
s = re.findall(\'[\u4e00-\u9fa5]\', data)# 比對所有漢字
kw = ("".join(s))
with open(\'temp.txt\', mode=\'w\', encoding=\'UTF-8\') as fw: # 打開檔案
fw.write(kw) #寫檔案
path = \'temp.txt\'
win32api.ShellExecute(0, \'open\', \'notepad.exe\', path, \'\', 1)
其中,mode參數為:r 表示 讀, w表示 寫, a表示 追加寫入
打開文檔也可用
import webbrowser as web
web.open(\'filepath or url\')
