EasyTable handles csv files as lists of lists. For example, if data.csv holds the following data...
1,2,3
4,5,6
7,8,9
...then EasyTable will handle it like this:
[[1,2,3],[4,5,6],[7,8,9]]
Knowing this format is critical to using the library correctly. The rows get condensed into lists, and the table is stored as a list of rows.
Installing EasyTable is super easy.
Download easytable.py below.
Drop the file into the same folder as your python project.
Include the line import easytable in your code.
That's it! You're done.
For the purpose of this explanation, the file data.csv is loaded for the DataTable object named dt.
The file contains the following data:
1,2,3
4,5,6
7,8,9
DataTable()
This is the constructor line. It indicates that you wish to create a data table object.
dt = easytable.DataTable()
getTable()
This method returns the table in the loaded csv file as a list of lists, as detailed above.
table = dt.getTable() # table = [[1,2,3],[4,5,6],[7,8,9]]
getItem(row,column)
This method returns the data stored in a single cell in the table. It will always return as a string!
item = dt.getItem(0,0) # item = '1'
item = dt.getItem(2,0) # item = '3'
item = dt.getItem(2,2) # item = '9'
getRow(row)
This method returns a single row as a list.
row = dt.getRow(1) # row = ['4','5','6']
row = dt.getRow(2) # row = ['7','8','9']
setRow(row,content)
This method will replace the contents of a single row with a list. Ensure that the length of the list matches the size of the table!
dt.setRow(1,['6','5','4'])
### data.csv now contains:
1,2,3
6,5,4
7,8,9
appendRow(row,content)
This method will add a row to the end of the table. Make sure it matches the size of the table! This will change the table size, so be careful with commands after this!
dt.appendRow(['10','11','12'])
### data.csv now contains:
1,2,3
4,5,6
7,8,9
10,11,12
loadTable("name.csv")
This method loads a particular csv file. If your file is in the same folder as your main python program, simply enter the name. Otherwise, to define a custom path, enter the full path.
dt.loadTable("data.csv")
setTable(data)
This method writes a list of lists to the table. This will overwrite any data stored in the table before, so use with caution!
dt.writeTable([['1','2','3'],['4','5','6']])
### data.csv will now contain:
1,2,3
4,5,6
setItem(row,column,content)
This method changes a single item in the table. It CANNOT add new items.
dt.setItem(0,1,"A")
### data.csv will now contain:
1,A,3
4,5,6
7,8,9
getCol(column)
This method returns a single column as a list.
col = dt.getCol(0) # col = ['1','4','7']
col = dt.getCol(2) # col = ['3','6','9']
setCol(column,content)
This method will replace the contents of a single column with a list. Ensure that the length of the list matches the size of the table!
dt.setCol(0,['a','b','c'])
### data.csv now contains:
a,2,3
b,5,6
c,8,9
appendCol(col,content)
This method will add a column to the end of the table. Make sure it matches the size of the table! This will change the table size, so be careful with commands after this!
dt.appendCol(['a','b','c'])
### data.csv now contains:
1,2,3,a
4,5,6,b
7,8,9,c