日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tsv文件转为Excel文件

發布時間:2024/3/24 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tsv文件转为Excel文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 需求
  • 思路
  • 實現

需求

現在有多個tsv文件,需要將其都轉換為excel文件

思路

  • 實現將一個文件轉換成功。將tsv文件按行讀取,然后通過xlsxwriter寫入新創建的xlsx文件中
  • 實現批量操作。這個過程比較簡單,就是遍歷指定目錄下的所有tsv文件,然后調用單個轉換程序。

實現

import csv import os from xlsxwriter.workbook import Workbook''' 將單個tsv文件轉為xlsx文件 ''' def tsv_to_xlsx(tsv_path_name):# Add some command-line logic to read the file names.tsv_file = tsv_path_namexlsx_file = os.path.splitext(tsv_path_name)[0] + '.xlsx'# Create an XlsxWriter workbook object and add a worksheet.workbook = Workbook(xlsx_file)worksheet = workbook.add_worksheet()# Create a TSV file reader.tsv_reader = csv.reader(open(tsv_file, 'rt'), delimiter='\t')# Read the row data from the TSV file and write it to the XLSX file.for row, data in enumerate(tsv_reader):worksheet.write_row(row, 0, data)# Close the XLSX file.workbook.close()def tsv_2_xlsx_dir(dir_path):for root, dirs, files in os.walk(dir_path):for file in files:#判斷后綴為tsv的if os.path.splitext(file)[1] == '.tsv':tsv_to_xlsx(root + "\\" + file)if __name__=="__main__":tsv_2_xlsx_dir('C:\\Users\\shand\\Desktop\\test')

總結

以上是生活随笔為你收集整理的tsv文件转为Excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。