tsv文件转为Excel文件
生活随笔
收集整理的這篇文章主要介紹了
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文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es6,js 数组截取并保留原数组
- 下一篇: PR3.0