Python 官方文档:入门教程 => 点击学习
目录前提提要要求思路代码运行结果分析1)读取文件2)读取数据3)数据整理4)正则表达式匹配外加数据去重6)数据导出保存前提提要 python2.0有无法直接读取中文路径的问题,需要另
python2.0有无法直接读取中文路径的问题,需要另外写函数。python3.0在2018年的时候也无法直接读取。
现在使用的时候,发现Python3.0是可以直接读取中文路径的。
需要自带或者创建几个txt文件,里面最好写几个数据(姓名,手机号,住址)
写代码的时候最好,自己设几个要求,明确下目的:
import glob
import re
import xlwt
filearray=[]
data=[]
phone=[]
filelocation=glob.glob(r'课堂实训/*.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')
会生成一个excel文件
import glob
import re
import xlwt
globe用来定位文件,re正则表达式,xlwt用于excel
filelocation=glob.glob(r'课堂实训/*.txt')
指定目录下的所有txt文件
for i in range(len(filelocation)):
file =open(filelocation[i])
file_data=file.readlines()
data.append(file_data)
print(data)
将路径下的txt文件循环读取,按序号依次读取文件
打开每一次循环对应的文件
将每一次循环的txt文件的数据按行读取出来
使用append()方法将每一行的数据添加到data列表中
输出一下,可以看到将几个txt的文件数据以字列形式存在同一个列表
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))
set()函数:无序去重,创建一个无序不重复元素集
#存到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')
到此这篇关于python文件数据分析治理提取的文章就介绍到这了,更多相关python文件数据分析内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python文件数据分析治理提取
本文链接: https://www.lsjlt.com/news/120155.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0