Python筆記 異常檔案讀寫2

2021-10-07 09:57:11 字數 1743 閱讀 8941

1、接收使用者鍵盤輸入的字串,統計字元出現的個數。

a=

input

("輸入:"

)count=

0for i in a:

count=count+

1print

(count)

import  re

b=open

(r"c:\users\pc-win10-22\desktop\html1"

)c=b.read(

)a=re.findall(

",c)

for i in a:

print

(i)

3、通過正規表示式,提取html1.txt中,所有標籤value的值

import re

b=open

(r"c:\users\pc-win10-22\desktop\html1"

)c=b.read(

)a=re.findall(

",c)

for i in a:

print

(i)

4、通過正規表示式,提取html1.txt中,所有標籤中同時含有 name,value的值

import re

b=open

(r"c:\users\pc-win10-22\desktop\html1"

)c=b.read(

)a=re.findall(

"<.*value.*/>|<.*name.*/>"

,c)for i in a:

print

(i)

5、通過正規表示式,提取html1.txt中,所有含有input的標籤

import re

b=open

(r"c:\users\pc-win10-22\desktop\html1"

)c=b.read(

)a=re.findall(

",c)

for i in a:

print

(i)

6、通過正規表示式,提取html1.txt中,表單標籤中所有的資料,寫入乙個檔案中。

import re

b=open

(r"c:\users\pc-win10-22\desktop\html1"

)c=b.read(

)a=re.findall(

"<.*/>"

,c)b=

open

(r"c:\users\asus\desktop\output\j.txt"

,"w"

)b.write(a)

b.close(

)

7、通過正規表示式,提前html1.txt中,所有表單的請求方式,以及請求位址

import re

b=open

(r"c:\users\pc-win10-22\desktop\html1"

)c=b.read(

)a=re.findall(

"method:(.*)"

,c)for i in a:

print

(i)b=re.findall(

"action:(.*)"

)for i in b:

print

(i)

python檔案讀寫,異常簡介

直接讀取 with在不需要訪問時自動關閉 開啟專案下的檔案,直接讀取 若要讀取指定檔案,則要輸入指定路徑 f pycharm project readwrite pi digits.txt with open pi digits.txt as file object contents file ob...

python檔案讀寫和異常

1,文字檔案 2,二進位制檔案 print type data with open 吉多.jpg wb as fs2 fs2.write data except filenotfounderror as e print 指定的檔案無法開啟.except ioerror as e print 讀寫檔案...

python檔案讀寫 異常處理

函式open 檔案讀寫,預設讀模式 open 路徑檔名.字尾名 eg file open d tes.txt content file.read print content file.close 讀 read 從文件開始位置讀取 readline 讀取一行內容,檔案指標在 就從 開始讀取一行 rea...