Python scripting - Common operators

CENPyOlpCsvParserOperator

Previous  Chapter  Next

 

The object of CENPyOlpCsvParserOperator class can be used in callback methods to communicate with .csv files.

 

[Example 1]

 

def PostProcessOperationAttributes(poa):

   csvParser = poa.GetCsvParserOperator()

 

   # Get the path to the TechTabs folder and CSV file:

   # Do not forget to add "import inspect, os" at the top of the file

   # currentPythonFilePath = os.path.abspath(inspect.getfile(inspect.currentframe()))

   # scriptsDirectoryPath = os.path.dirname(currentPythonFilePath)

   # parentDirectoryPath = os.path.dirname(scriptsDirectoryPath)

   # csvFilePath = str(parentDirectoryPath + "\\TechTabs\\Test.csv")

   # or simply:

   csvFilePath = str(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) + "\\TechTabs\\Test.csv")

 

   iRet = csvParser.LoadCsvFile(csvFilePath)

   if iRet == 0:

      logging.LogInfo("Successfully imported CSV file: " + str(csvFilePath))

      csvParser.SetSeparator(",")

      numberOfRows = csvParser.GetNumberOfRows()

      numberOfColumns = csvParser.GetNumberOfColumns()

      cell12 = csvParser.GetCell(1, 2)   

      cellB1 = csvParser.GetCellByColumnName('B', 2)

      row1 = csvParser.GetRow(1)

      row2 = csvParser.GetRow(2)

 

# expected result:

# numberOfRows = 4; numberOfColumns = 4, cell12 = 'a1', cellB1 = 'b1'

row1 = ['A','b','c']

row2 = ['a1','b1','c1']

csv_table_sample

 

 

Methods

 

LoadCsvFile(filepath: string)

 

o

filepath

absolute path to the csv file

 

[Example]

   # Get the path to the TechTabs folder and CSV file:

   # Do not forget to add "import inspect, os" at the top of the file

   # currentPythonFilePath = os.path.abspath(inspect.getfile(inspect.currentframe()))

   # scriptsDirectoryPath = os.path.dirname(currentPythonFilePath)

   # parentDirectoryPath = os.path.dirname(scriptsDirectoryPath)

   # csvFilePath = str(parentDirectoryPath + "\\TechTabs\\Test.csv")

   # or simply:

   csvFilePath = str(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) + "\\TechTabs\\Test.csv")

 

   iRet = csvParser.LoadCsvFile(csvFilePath)

   if iRet == 0:

      logging.LogInfo("Successfully imported CSV file: " + str(csvFilePath))

 


 

GetNumberOfRows() : Int

 

o

return

Number of rows in the table

 

[Example]

   numberOfRows = csvParser.GetNumberOfRows()

 


 

GetNumberOfColumns() : Int

 

o

return

The number of columns of the file

 

[Example]

   numberOfColumns = csvParser.GetNumberOfColumns()

 


 

GetCell(columnIndex: unsigned int, rowIndex: unsigned int) : string

 

o

columnIndex

The column-index.

 

o

rowIndex

The row-index.

 

o

return

Content of cell

 

The content of the table at column

 

[Example]   

   cell12 = csvParser.GetCell(1, 2)   

 


 

GetCellByColumnName(columnName: string, rowIndex :  unsigned int) : string

 

o

columnName

The column-name.

 

o

rowIndex

The row-index.

 

o

return

Content of cell

 

Accessing a cell by column-name(e.g. A, B, .., AA, AB,..) and a row-index.

 

[Example]   

   cellB1 = csvParser.GetCellByColumnName('B', 2)

 


 

SetSeparator(separator: string)

 

o

separator

Text of the message

 

Setting the separator-character in the csv-file.

Remark: The default separator is given by ';'.

 

[Example]   

   csvParser.SetSeparator(",")

 


 

GetRow(rowIndex: unsigned int) : list

 

o

separator

Text of the message

 

o

return

a list of cell values in the row

 

[Example]      

   row1 = csvParser.GetRow(1)

 


 

 


Previous
Previous page
Chapter
Chapter page
Next
Next page