简体   繁体   English

将Excel写入CSV文件,如何使用另一个定界符?

[英]Writing Excel to CSV file, how can I use another delimiter?

So im using the following script to export a excel file to csv. 因此,即时通讯使用以下脚本将excel文件导出到csv。

import csv
import xlrd

workbook = xlrd.open_workbook('excel.xlsm')
for sheet in workbook.sheets():
    if sheet.name == "Database":
        with open('{}.csv'.format(sheet.name), 'wb') as f:
            writer = csv.writer(f)
            for row in range(sheet.nrows):
                out = []
                for cell in sheet.row_values(row):
                    if isinstance(cell, float):
                        out.append(cell)
                    else:
                        out.append(unicode(cell).encode('utf8'))
                writer.writerow(out)

So far everything it's working good. 到目前为止,一切工作正常。 but I would like the csv file to be created with ; 但我想用创建csv文件; as a delimiter for each cell instead of the , How can I acheive this? 作为每个单元格的分隔符,而不是,我如何实现呢?

instead of cell1,cell2, I want the output to become cell1;cell2; 我希望输出改为cell1;cell2;而不是cell1,cell2, cell1;cell2;

(Edit) Solution (编辑)解决方案

set the delimiter when initiating csv.writer writer = csv.writer(f, delimiter=";") 在启动csv.writer writer = csv.writer(f, delimiter=";")时设置定界符

找到答案,可以在启动csv.writer时分配定界符

writer = csv.writer(f, delimiter=";")

暂无
暂无

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

相关问题 如何使用python更改csv文件的定界符,同时还剥离新定界符的字段? - How can I use python to change the delimiter of a csv file while also stripping the fields of the new delimiter? 在导入带有额外逗号的熊猫的csv文件时,如何使用正则表达式作为分隔符? - How can I use regex as a delimiter when importing a csv file with pandas with extra commas? 如何将字符串解析为 python 格式的 CSV 格式? 实际上并未写入 CSV 文件,无法使用 csv 库 - How can I parse a string into CSV format in python? Not actually writing to CSV file, can't use csv library 如何避免覆盖写入数据的 csv 文件? - How can I avoid overwriting of csv file writing the data into? 我可以导入 CSV 文件并自动推断分隔符吗? - Can I import a CSV file and automatically infer the delimiter? 如何在 Python 中为 CSV 使用分隔符? - How to use delimiter for CSV in Python? 如何使用熊猫在最后一个字段中存在分隔符的情况下读取CSV文件? - How to use Pandas to read CSV file with delimiter existing in the last field? 如何在 python 中使用 CSV 文件? - How can I use a CSV file in python? 如何将分隔符添加到打印 function 并在 excel 文件的特定单元格中打印? - How can I add a delimiter to the print function to and print within a specific cell in an excel file? 如何读取以 µ 作为分隔符的 txt 文件? - How can I read a txt file with µ as delimiter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM