ExperimentSession.read_csv_todict()#
- ExperimentSession.read_csv_todict(path: Union[str, pathlib.Path], encoding: str = 'utf-8', **kwargs) Iterator[dict] [source]#
Iterates over the rows in a .csv file, yielding dictionaries.
- 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.DictReader
- Yields
dict – A dictionary in which the keys are the column names.
Examples
Consider the following csv-file, located at
files/data.csv
in your experiment directory: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_todict("files/data.csv"): print(row)
The output would be the following:
{"col1": "text_a", "col2": "text_b", "col3": "text_c"} # first iteration {"col1": "text_d", "col2": "text_e", "col3": "text_f"} # second 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_todict("files/data.csv")) print(data)
The output would be the following:
[{"col1": "text_a", "col2": "text_b", "col3": "text_c"}, {"col1": "text_d", "col2": "text_e", "col3": "text_f"}]