简体   繁体   English

如何通过Robot Framework或python脚本将csv(逗号分隔)或xlsx(excel)文件转换为psv(管道分离)文件

[英]How to convert csv(comma separated) or xlsx(excel) file to psv (pipe separated ) file through Robot Framework or python scripting

我有一个文件,可以是csv文件或xlsx文件,如何通过Robot框架或python脚本将此文件转换为PSV文件

From a CSV file to "psv" file through direct Python scripting using the csv module: 使用csv模块通过直接Python脚本从CSV文件到“psv”文件:

import csv

with open('input.csv', 'rU') as infile, open('output.psv', 'w') as outfile:
    reader = csv.reader(infile)
    writer = csv.writer(outfile, delimiter='|')
    writer.writerows(reader)

From .xlsx file to "psv" using the xlrd package: 使用xlrd包从.xlsx文件到“psv”:

import csv
import xlrd

workbook = xlrd.open_workbook('input.xlsx')
sheet = workbook.sheet_by_index(0)        # assume that the data is in the first sheet
with open('output.psv', 'w') as outfile:
    writer = csv.writer(outfile, delimiter='|')
    for i in range(sheet.nrows):
        writer.writerow([cell.value for cell in sheet.row(i)])

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

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