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

时间:2014-10-14 11:29:13

标签: python selenium robotframework

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

1 个答案:

答案 0 :(得分:3)

从CSV文件到" psv"使用csv模块通过直接Python脚本编写文件:

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)

.xlsx文件到" psv"使用xlrd包:

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)])