網(wǎng)站首頁 編程語言 正文
前提提要
python2.0有無法直接讀取中文路徑的問題,需要另外寫函數(shù)。python3.0在2018年的時候也無法直接讀取。
現(xiàn)在使用的時候,發(fā)現(xiàn)python3.0是可以直接讀取中文路徑的。
需要自帶或者創(chuàng)建幾個txt文件,里面最好寫幾個數(shù)據(jù)(姓名,手機號,住址)
要求
寫代碼的時候最好,自己設(shè)幾個要求,明確下目的:
- 需要讀取對應(yīng)目錄路徑的所有對應(yīng)文件
- 按行讀取出每個對應(yīng)txt文件的記錄
- 使用正則表達式獲取每行的手機號
- 將手機號碼存儲到excel中
思路
- 1)讀取文件
- 2)讀取數(shù)據(jù)
- 3)數(shù)據(jù)整理
- 4)正則表達式匹配
- 5)數(shù)據(jù)去重
- 6)數(shù)據(jù)導(dǎo)出保存
代碼
import glob
import re
import xlwt
filearray=[]
data=[]
phone=[]
filelocation=glob.glob(r'課堂實訓(xùn)/*.txt')
print(filelocation)
for i in range(len(filelocation)):
file =open(filelocation[i])
file_data=file.readlines()
data.append(file_data)
print(data)
combine_data=sum(data,[])
print(combine_data)
for a in combine_data:
data1=re.search(r'[0-9]{11}',a)
phone.append(data1[0])
phone=list(set(phone))
print(phone)
print(len(phone))
#存到excel中
f=xlwt.Workbook('encoding=utf-8')
sheet1=f.add_sheet('sheet1',cell_overwrite_ok=True)
for i in range(len(phone)):
sheet1.write(i,0,phone[i])
f.save('phonenumber.xls')
運行結(jié)果
會生成一個excel文件
分析
import glob
import re
import xlwt
globe用來定位文件,re正則表達式,xlwt用于excel
1)讀取文件
filelocation=glob.glob(r'課堂實訓(xùn)/*.txt')
指定目錄下的所有txt文件
2)讀取數(shù)據(jù)
for i in range(len(filelocation)):
file =open(filelocation[i])
file_data=file.readlines()
data.append(file_data)
print(data)
將路徑下的txt文件循環(huán)讀取,按序號依次讀取文件
打開每一次循環(huán)對應(yīng)的文件
將每一次循環(huán)的txt文件的數(shù)據(jù)按行讀取出來
使用append()方法將每一行的數(shù)據(jù)添加到data列表中
輸出一下,可以看到將幾個txt的文件數(shù)據(jù)以字列形式存在同一個列表
3)數(shù)據(jù)整理
combine_data=sum(data,[])
列表合并成一個列表
4)正則表達式匹配外加數(shù)據(jù)去重
print(combine_data)
for a in combine_data:
data1=re.search(r'[0-9]{11}',a)
phone.append(data1[0])
phone=list(set(phone))
print(phone)
print(len(phone))
set()函數(shù):無序去重,創(chuàng)建一個無序不重復(fù)元素集
6)數(shù)據(jù)導(dǎo)出保存
#存到excel中
f=xlwt.Workbook('encoding=utf-8')
sheet1=f.add_sheet('sheet1',cell_overwrite_ok=True)
for i in range(len(phone)):
sheet1.write(i,0,phone[i])
f.save('phonenumber.xls')
- Workbook('encoding=utf-8'):設(shè)置工作簿的編碼
- add_sheet('sheet1',cell_overwrite_ok=True):創(chuàng)建對應(yīng)的工作表
- write(x,y,z):參數(shù)對應(yīng)行、列、值
原文鏈接:https://blog.51cto.com/u_15763679/5615056
相關(guān)推薦
- 2022-11-23 Python?threading模塊中l(wèi)ock與Rlock的使用詳細講解_python
- 2022-04-21 Python?數(shù)據(jù)類型--集合set_python
- 2023-01-14 Android?ANR無響應(yīng)分析解決方案_Android
- 2022-05-24 Go中的新增對模糊測試的支持_Golang
- 2022-10-16 QT編寫tcp通信工具(Client篇)_C 語言
- 2022-04-21 Tomcat解決catalina.out文件過大的問題_Tomcat
- 2022-03-11 UE4 添加自己項目的AutomationProject,解決報錯Failed to find co
- 2022-12-12 C語言實現(xiàn)循環(huán)打印星號圖形再鏤空_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支