Python文件数据分析提取怎样实现,有什么要注意
Admin 2022-09-07 群英技术资讯 632 次浏览
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')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了利用Python删除电脑中的重复文件,下文我们来分享解决电脑中文件重复的情况的一个方法,需要的朋友可以参考一下,希望对大家日常问题解决有所帮助
在本篇文章里小编给大家整理的是一篇关于pygame实现方块动画实例讲解内容,以后需要的朋友们可以学习参考下。
python取对数可以采用两种工具包,math包可对单一数字取对数,numpy可以数列整体取对数。
这篇文章介绍了Python使用pyecharts控件绘制图表的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要为大家详细介绍了Python使用sftp实现上传和下载功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008