Hi everyone, I’m currently trying to run a study where I save data “manually” using code components. After each trial, I save the data like this:
thisExp.addData('my_column_name', "my_column_entry") # save data
thisExp.nextEntry() # go to new row
This works fine most of the time, but sometimes data from multiple rows are saved in one and it’s causing quite a mess in my csv (see screenshot).
I can’t really see why this is happening though. It seems to happen if one of my text variables contains a quote, but it’s not always the case. I also escape all quote characters in my strings, so I really don’t know why some of them should cause an issue.
Does anyone have an idea what’s going on there?
OS (e.g. Win10): Mac Os Monterey 12.0.1
PsychoPy version: 2021.2.3 (I need to use this one, I know there’s a newer one)
Okay, I found out what’s causing this, just in case anyone else is running into this issue, too: Apparently, when you try saving strings in CSV files, you have to be really careful about how you escape quotes. In my case, I escaped them using backslashes, but that didn’t work when I saved the data, and the un-escaped quotes messed up my CSV.
This is how I solved it:
I created a small function that’s replacing any quotes in the string with double quotes, because apparently, you need to escape quotes with quotes instead of a backslash.
For example, if I have the string
"example\"", I need to convert it to
"example""". And yes, this looks completely ridiculous.
Then I use the function for each string that might contain quotes when saving it, like so:
return string.replace('"', '""')