【问题标题】:How to convert csv(comma separated) or xlsx(excel) file to psv (pipe separated ) file through Robot Framework or python scripting如何通过 Robot Framework 或 python 脚本将 csv(逗号分隔)或 xlsx(excel)文件转换为 psv(管道分隔)文件
【发布时间】:2014-12-09 04:24:47
【问题描述】:

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

【问题讨论】:

    标签: python selenium robotframework


    【解决方案1】:

    使用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)
    

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

    【讨论】:

    • 由于问题是关于在机器人框架中使用这个,你可能想要包含一些关于如何使用这个代码作为关键字的内容。
    • @BryanOakley:问题是机器人框架还是直接 python。我不知道前者,但如果你知道,请添加评论或答案。
    • 谢谢,我在 Ride 中添加了实现上述 python 代码的关键字,它可以工作
    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    相关资源
    最近更新 更多