我有一个文件,可以是csv文件或xlsx文件,如何通过Robot框架或python脚本将此文件转换为PSV文件
答案 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)])