1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python实现dat文件批量转换为 excel文件 关键词 批量格式转换 dat文件 excel

python实现dat文件批量转换为 excel文件 关键词 批量格式转换 dat文件 excel

时间:2020-12-25 16:07:19

相关推荐

python实现dat文件批量转换为 excel文件 关键词 批量格式转换 dat文件   excel

python实现批量dat转excel.pyimport os,time,xlwtdef single(filepath):'''参数:txt文件路径,功能将其转换成excel文件'''print(filepath)data = []newname= filepath.replace(".txt",".xls")print("+++++++++++++++++++++++++++++")print(newname)fp = open(filepath,"r",encoding="utf-8") for line in fp: #设置文件对象并读取每一行文件# print(type(line)) # strline = line.split('|@|')data.append(line)row_num = len(data)col_num = len(data[0]) #如果是空文件此处报错# 步骤2:创建工作簿对象workbookworkbook = xlwt.Workbook(encoding='utf-8')# 步骤3:创建单页对象sheetsheet = workbook.add_sheet('测试单页1')# 步骤5:写入内容数据# 步骤5-1:外层for循环控制行数for rowIndex in range(0, row_num):# 步骤5-2:内层for循环控制列数for colIndex in range(col_num):# 步骤5-3:写入内容数据sheet.write(rowIndex, colIndex, data[rowIndex][colIndex])# 步骤6:保存工作簿workbook.save(newname)def dat_txt():'''当前文件夹,文件后缀dat批量更改为txt'''files = os.listdir('.') # 列出当前目录下所有的文件# print('files',files)for filepath in files:portion = os.path.splitext(filepath)if portion[1] == ".dat": # 如果后缀是.dat# 重新组合文件名和后缀名newname = portion[0] + ".txt" os.rename(filepath,newname)def get_txtpath():'''获取每个txt文件的路径'''files = os.listdir('.')# print('files',files)data=[]for filename in files:portion = os.path.splitext(filename)# 如果后缀是.datif portion[1] == ".txt":filepath = os.getcwd()+ os.sep + filenamedata.append(filepath) print("++++++++++++ data +++++++++++++++")print(data)return data dat_txt() #dat后缀批量更改为txtdata = get_txtpath()# 获取每个txt文件的路径for i in data:fileSize = os.path.getsize(i)printif fileSize != 0:filepath = isingle(filepath)

python实现dat文件批量转换为 excel文件 关键词 批量格式转换 dat文件 excel读写 诺祺 可发表知乎ed csdn ed

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。