当前位置:   article > 正文

pandas实现csv中数据的遍历_pandas遍历csv

pandas遍历csv

实现的datas数据是这样的:
在这里插入图片描述
任务就是把里面的数据全部都取到,并且对数据可以进行处理。

data文件就保存在当前同级别的
datas内容:

SheetName,Step,Behavior,OperationPage,Description,ObjectName,PositioningExpression,InputValue,OutputValue,ExpectedResult,WaitTime,Enabled,IgnoreErr,LoopCnt
case_one,1,openurl,Logon,打开浏览器,Stadd,,http://mozilla.com.cn/moz-portal.html,,,1,True,True,1
case_one,2,refresh,Logon,刷新,Stadd,,,,,10,True,True,1
case_one,3,click,Logon,电影,Button,/html/body/div[6]/div[1]/div/div/form/div/div[1]/a[1],,,,2,True,True,1
case_one,4,quit,,close webdrive,,,,,,2,True,True,1
  • 1
  • 2
  • 3
  • 4
  • 5

话不多说,上代码!

import pandas as pd
import os


class DoFile:
    def get_excellist(self):
        file_list = []
        root_dir = os.getcwd()
        dir_or_files = os.listdir(root_dir)
        for dir_file in dir_or_files:
            dir_file_path = os.path.join(root_dir, dir_file)
            if os.path.isdir(dir_file_path):
                pass
            else:
                filepath, filename = os.path.split(dir_file_path)
                file_list.append(filename)
                case_list = list(filter(lambda case: ".csv" in case, file_list))
        if not case_list:
            raise Exception(self.log.logMsg(3, ("case not exist")))
        return case_list


class CaseStep:

    def __init__(self):
        self.SheetName = ''
        self.Step = 0
        self.Behavior = ''
        self.OperationPage = ''
        self.Description = ''
        self.ObjectName = ''
        self.PositioningExpression = ''
        self.InputValue = ''
        self.OutputValue = ''
        self.ExpectedResult = ''
        self.WaitTime = 1
        self.Enabled = 1
        self.IgnoreErr = 1
        self.LoopCnt = 1


class Case:
    def __init__(self):
        self.caseSteps = list()
        self.elementMaps = list()


class ExcelCaseReader:

    def __init__(self):
        self.excel = DoFile()

    def read_case(self):
        """ read case file and return case """
        case = Case()
        csv_list = self.excel.get_excellist()
        print('*'*30,f'{csv_list}')
        for xlsx in csv_list:
            if "~$" in xlsx:
                pass
            else:
                csv = pd.read_csv(xlsx)
                # print(pd.read_csv(xlsx).loc[0].to_dict())
                df = pd.DataFrame(csv)
                self.df = df
                for value in range(len(df.index)):
                    case_step = CaseStep()
                    case_step.SheetName = df['SheetName'][value]
                    case_step.Step = df['Step'][value]
                    case_step.Behavior = df['Behavior'][value]
                    case_step.OperationPage = df['OperationPage'][value]
                    case_step.Description = df['Description'][value]
                    case_step.ObjectName = df['ObjectName'][value]
                    case_step.PositioningExpression = df['PositioningExpression'][value]
                    case_step.InputValue = df['InputValue'][value]
                    case_step.OutputValue = df['OutputValue'][value]
                    case_step.ExpectedResult = df['ExpectedResult'][value]
                    case_step.WaitTime = df['WaitTime'][value]
                    case_step.Enabled = df['Enabled'][value]
                    case_step.IgnoreErr = df['IgnoreErr'][value]
                    case_step.LoopCnt = df['LoopCnt'][value]
                    case.caseSteps.append(case_step)
        return case


if __name__ == '__main__':
    case = ExcelCaseReader().read_case()
    for i in case.caseSteps:
        print(i.Description)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

那么运行的结果如下:

****************************** ['datas.csv']
打开浏览器
刷新
电影
close webdrive
  • 1
  • 2
  • 3
  • 4
  • 5

补充接口的:

CsvName,Step,Description,Method,Url,Grant_type,Client_id,Client_secret,Username,Password,WaitTime,Enabled,IgnoreErr
CsvName,1,demo1,get,/wmr-auth/v1/wmr-token,password,cfss_wmr,secret,100557,0000abc!,1,0,0
CsvName,2,demo2,post,/wmr-auth/v1/wmr-token,password,cfss_wmr,secret,105565,0000abc!,2,0,0
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/210218
推荐阅读
相关标签
  

闽ICP备14008679号