python操作c's'v及xls(excel)文件

本文介绍了如何使用Python的csv模块处理CSV文件,并通过第三方库xlwt和xlrd进行Excel(xls)文件的操作,涵盖了读写数据的基本步骤。
摘要由CSDN通过智能技术生成

1. 操作csv,使用python的csv模块

操作如下数据表csv格式
这里写图片描述

# -*- coding: utf-8 -*-
"""
Created on Sun May 20 13:04:27 2018

@author: spfhy
"""
import csv

class OpTestConfig:
    def __init__(self,ops,case_name,param,run_mode,run_type):
        self.ops = ops
        self.case_name = case_name
        self.param = param
        self.run_mode = run_mode
        self.run_type = run_type
    def opTestGenerateCmd(self):
        cmd = self.ops + ' ' + self.case_name + ' '\
              + self.run_mode + ' ' + self.param + ' ' + self.run_type
        return cmd
#解析config配置文件
binFilePath = ''
run_mode = 'IPU'
run_type = '3'
with open("config.csv", "r", encoding = "utf-8") as f:
    reader = csv.reader(f)
    for row in reader:
        if row[0] == 'filepath':
            binFilePath = row[1]
            print('filepath : '+ binFilePath)
        elif row[0] == 'run_mode':
            run_mode = row[1]
            print('run_mode : '+run_mode)
        elif row[0] == 'run_type':
            run_type = row[1] 
            print('run_type : ' + run_type)
f.close()
#解析test_case列表

OpTestlist = []
with open("test_xlwt.csv", "r", encoding = "utf-8") as f:
    reader = csv.DictReader(f)
    for row in reader:
        if row['select'] == 'yes':
            #print(row)
            Optest = OpTestConfig(row['ops'],row['case_name'],row['param'],run_mode,run_type)
            OpTestlist.append(Optest)
f.close()
test_case_num = len(OpTestlist)
if test_case_num > 0:
    print('case num is :' + str(test_case_num))
    for i in range(test_case_num):
        print(str(i) + ': '+ OpTestlist[i].opTestGenerateCmd())
else:
    print('no case to run')

2. 操作xsl,使用python的csv模块

使用python的第三方库xlwt,xlrd操作excel文件

# -*- coding: utf-8 -*-
"""
Created on Sat May 19 10:54:25 2018

@author: spfhy
"""
'''
写xml
import xlwt

workbook = xlwt.Workbook(encoding='utf-8')  
booksheet = workbook.add_sheet('test case', cell_overwrite_ok=True)  
#存第一行cell(1,1)和cell(1,2)  
booksheet.write(0,0,'序号')  
booksheet.write(0,1,'算子')
booksheet.write(0,3,'testcast') 
booksheet.write(0,4,'run_type')     
#存第二行cell(2,1)和cell(2,2)  
booksheet.write(1,0,36)  
booksheet.write(1,1,39)  
#存一行数据  
rowdata = [43,56]  
for i in range(len(rowdata)):  
    booksheet.write(2,i,rowdata[i])  
workbook.save('test_xlwt.xls')  
'''
'''
读xml
'''
import xlrd

class OpTestConfig:
    def __init__(self,ops,case_name,param,run_mode,run_type):
        self.ops = ops
        self.case_name = case_name
        self.param = param
        self.run_mode = run_mode
        self.run_type = run_type
    def opTestGenerateCmd(self):
        cmd = self.ops + ' ' + self.case_name + ' '\
              + self.run_mode + ' ' + self.param + ' ' + self.run_type
        return cmd

#解析config配置文件
binFilePath = ''
run_mode = ''
run_type = ''

booksheet = xlrd.open_workbook('test_xlwt.xls')
table = booksheet.sheet_by_name('config')
nrows = table.nrows
for row in range(nrows):
    if table.cell(row,0).value == 'bin_so_filepath':
        binFilePath = table.cell(row,1).value
    elif table.cell(row,0).value == 'run_mode':
        run_mode = table.cell(row,1).value
    elif table.cell(row,0).value == 'run_type':
        run_type = str(table.cell(row,1).value)

#解析test_case列表        
table = booksheet.sheet_by_name('test_case')
ncols = table.ncols
chpice_col = 0
ops_col = 0
case_col = 0
run_type_col = 0
for col in range(ncols):
    cellValue = table.cell(0,col).value
    print(cellValue)
    if(cellValue == 'select'):
        chpice_col = col
    elif(cellValue == 'ops'):
        ops_col = col
    elif(cellValue == 'case_name'):
        case_col = col  
    elif(cellValue == 'param'):
        param_col = col 

OpTestlist = []
for i in range(table.nrows):
    if(table.cell(i,chpice_col).value == 'yes'):
        ops = table.cell(i,ops_col).value
        case_name = table.cell(i,case_col).value
        param = table.cell(i,param_col).value
        Optest = OpTestConfig(ops,case_name,param,run_mode,run_type)
        OpTestlist.append(Optest)

oplist_len = len(OpTestlist)
if oplist_len > 0:
    print(str(oplist_len) + '  testcase need to run')
    for i in range(oplist_len):
        print(str(i)+ ': ' + OpTestlist[i].opTestGenerateCmd())
else:
    print('no testcase need to run')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值