# 从一堆数字和名字中把学生的学号按顺序给列出来 import re import numpy as np import pandas as pd ''' a = re.findall(r"\d+", '1小时12分钟') # 匹配数字 b = re.findall(r"\D+", '1小时12分钟') # 匹配非数字 print(a) print(b) ''' with open(r"D:\水泵及水泵站课程设计资料\name.txt", encoding='gbk', errors='ignore') as f: # 打开文件 data = f.read() # 读取文件,所有数据 print(data) # 查看数据 list = re.findall(r"\d+", data) # 匹配数字,把数字提取出来 print(list) print(type(list)) # 查看list的类型 class 为 list list2 = np.array(list) # 转换为numpy print(type(list2)) # 查看list2的类型 class 为 numpy.ndarray print(len(list2)) # 长度162 list3 = list2.reshape(54,3) # 改变形状 list4 = list3[:,0] print(list4) list5 = list4.reshape(54,1) print(type(list5)) print(list5) # #将numpy数组写入Excel data = pd.DataFrame(list5) writer = pd.ExcelWriter(r"D:\水泵及水泵站课程设计资料\name1.xlsx") # 写入Excel文件 data.to_excel(writer, 'page_1', float_format='%.5f') # ‘page_1’是写入excel的sheet名 writer.save() writer.close()