ExperimentSession.read_csv_tolist()#
- ExperimentSession.read_csv_tolist(path: Union[str, pathlib.Path], encoding: str = 'utf-8', **kwargs) Iterator[list] [source]#
Iterates over the rows in a .csv file, yielding lists.
- Parameters
path – The path to the .csv file. Usually, you want this to be a relative path to a file in a subdirectory of the experiment directory.
encoding – Encoding of the .csv file. Defaults to ‘utf-8’.
**kwargs – Further arguments passed on to
csv.reader
- Yields
list – A list of the values in one row.
Examples
Consider the following csv-file:
col1 , col2 , col3 text_a , text_b , text_c text_d , text_e , text_f
When building a page, usual usage would be:
import alfred3 as al exp = al.Experiment() @exp.member class CSVDemoPage(al.Page): # this could also be a Section name = "csv_demo" def on_exp_access(self): for row in self.exp.read_csv_tolist("files/data.csv"): print(row)
The output would be the following:
["col1", "col2", "col3"] # first iteration yields column names ["text_a", "text_b", "text_c"] # second iteration ["text_a", "text_b", "text_c"] # third iteration
If you need a full list of the rows, you can wrap the function call in
list()
:import alfred3 as al exp = al.Experiment() @exp.member class CSVDemoPage(al.Page): # this could also be a Section name = "csv_demo" def on_exp_access(self): data = list(self.exp.read_csv_tolist("files/data.csv")) print(data)
The output would be the following:
[["col1", "col2", "col3"], ["text_a", "text_b", "text_c"], ["text_a", "text_b", "text_c"]]