oTree Forum >

Accessing participant id_in_session

#1 by Juliana


I need to use the participant id_in_session variable in my app to read data from a csv file based on the participant's id_in_session. 

In my csv file, one column is id_in_session and takes values 1, 2, 3, etc. I want the program to read data from the csv file, corresponding to the participant id_in_session. 

Does anyone have insights on how to solve this problem?

Thank you!

def read_worker_database(id_in_session):
    f = open('_static/csv_files/worker_data.csv', encoding='utf-8-sig')
    rows = [row for row in csv.DictReader(f)]
    worker_answers = ["worker_q1", "worker_q2", "worker_q3", "worker_q4"]
    selected_answer = random.choice(worker_answers)
    allocated_answer = {selected_answer}

    for row in rows:
        if row["id_in_session"] == id_in_session:
            allocated_answer = row[selected_answer]
    return selected_answer, allocated_answer

class Intro(Page):
    form_model = 'player'
    def before_next_page(player: Player, timeout_happened):
        allocated_answer = read_worker_database(player.participant.id_in_session)

#2 by Juliana

This code works if I use the participant code, instead of the participant id in session, which suggests the problem comes from using id in session. 

Thank you!

#3 by Chris_oTree

Because id_in_session is an integer like 42, whereas the CSV stores everything as a string like "42".

Write a reply

Set forum username