简体   繁体   English

将Excel范围转换为csv或制表符分隔的文件

[英]Convert Excel range into csv or tab-delimited file

I have an Excel 2016 worksheet Sheet1 and would like to convert the range $A$1:$Z$150 into a csv or tab-delimited file. 我有一个Excel 2016工作表Sheet1 ,想将$A$1:$Z$150转换为csv或制表符分隔的文件。

I am using python v3 xlwings to capture the range. 我正在使用python v3 xlwings捕获范围。

my_range = Range('Sheet1','A1:Z150').value

How do I convert my_range into a csv or tab-delimited file using python? 如何使用python将my_range转换为csv或制表符分隔的文件?

A tab-delimited file is same as copying the range and pasting it onto a text file. 制表符分隔的文件与复制范围并将其粘贴到文本文件相同。 This is actually my preference. 这实际上是我的偏爱。

If my understanding of xlwings is correct, then my_range should be a nested list object. 如果我对xlwings的理解是正确的,则my_range应该是嵌套列表对象。 In this case the following code suffices to write the data to a CSV file with tabs as delimiter. 在这种情况下,以下代码足以使用制表符作为分隔符将数据写入CSV文件。

import csv

with open('data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter='\t')
    writer.writerows(my_range)

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

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