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')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM