简体   繁体   English

如何使用 Python 将 Excel 工作表写入制表符分隔的 .txt 文件

[英]How to write an Excel sheet to a tab separated .txt file with Python

I've got a program that's converting files from an Excel spreadsheet into a csv.我有一个程序可以将文件从 Excel 电子表格转换为 csv。 However, I just realized that for one of the other things I'm working on, I need them to be tab separated .txt files.但是,我刚刚意识到,对于我正在处理的其他事情之一,我需要它们是制表符分隔的 .txt 文件。 How can I either convert what it's kicking out, or change it to do tab separated instead?我该如何转换它踢出的内容,或者将其更改为以制表符分隔?

import openpyxl
import csv

wb = openpyxl.load_workbook('S:\\Digitization\\Metadata\\MS038_Civitan.xlsx', data_only = True)
sh1 = wb["MODS"]
with open('S:\\Digitization\\Metadata\\tools\\CSVs\\MODS.csv', 'wb') as f:
    c = csv.writer(f)
    for r in sh1.rows:
        out_val_list = list()
        for cell in r:
            out_val = cell.value
            if out_val == 0:
                out_val = ""
            out_val_list.append(out_val)
        c.writerow(out_val_list)

You can choose your delimiter like this您可以像这样选择分隔符

csv.writer(f, delimiter='\\t')

https://docs.python.org/3.7/library/csv.html#examples https://docs.python.org/3.7/library/csv.html#examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM