最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python文件數(shù)據(jù)分析治理提取

 更新時間:2022年08月24日 14:53:03   作者:wx6305967491b61???????  
這篇文章主要介紹了python文件數(shù)據(jù)分析治理提取,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

前提提要

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文件的記錄
  • 使用正則表達(dá)式獲取每行的手機號
  • 將手機號碼存儲到excel中

思路

  • 1)讀取文件
  • 2)讀取數(shù)據(jù)
  • 3)數(shù)據(jù)整理
  • 4)正則表達(dá)式匹配
  • 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正則表達(dá)式,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)正則表達(dá)式匹配外加數(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)行、列、值

到此這篇關(guān)于python文件數(shù)據(jù)分析治理提取的文章就介紹到這了,更多相關(guān)python文件數(shù)據(jù)分析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

涡阳县| 岳阳县| 贡山| 个旧市| 墨江| 旬邑县| 始兴县| 全州县| 钟山县| 临朐县| 凤庆县| 阿拉善左旗| 于都县| 富宁县| 固阳县| 来凤县| 白城市| 海口市| 景东| 霍州市| 上杭县| 马鞍山市| 佳木斯市| 安龙县| 福海县| 台安县| 蒙城县| 泌阳县| 当阳市| 渝北区| 合水县| 密云县| 武陟县| 肥城市| 全椒县| 定远县| 克什克腾旗| 林芝县| 简阳市| 忻城县| 漯河市|