Python Forum
Help with Code (Writing Data File)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Code (Writing Data File)
#4
(Jan-06-2019, 02:50 AM)scidam Wrote: I just installed psychopy with lots of its dependencies...
Everything works fine until line 30.
If you looked at startInfo , you could find that it is just a list of strings.
So, the dataFile variable is a string too. Attempting to call nonexistent .write method (line #32) for the string
rises the error. I got the following:
Error:
Traceback (most recent call last): File "test.py", line 32, in <module> dataFile.write(",".join(dataHeader) + "\n") AttributeError: 'str' object has no attribute 'write'
If you want to write data into a csv file, I would suggest you to use the Pandas package,
data frames and magic method .to_csv. Using Pandas wouldn't be an overkill, if we take into
account tons of dependencies of the pychopy package:)

Alternative way is to create file and write to it directly, e.g. insert the
following piece of code instead of lines #30-33:

dataHeader=["Participant","Age","Gender","Handedness","Trial","Cue Side", "SOA","Target Type","RT"]
with open("mydata.csv", "w") as dataFile:
    dataFile.write(",".join(dataHeader) + "\n")
Note that I've never used psychopy before. So, it might also include
its own mechanism of storing data to files and we don't need to build such things from scratch.

Thanks, this was very helpful!
Reply


Messages In This Thread
RE: Help with Code (Writing Data File) - by scidam - Jan-06-2019, 02:50 AM
RE: Help with Code (Writing Data File) - by caroline_d_124 - Jan-06-2019, 09:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 332 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 1 416 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 3,105 Dec-06-2022, 11:09 AM
Last Post: mg24
  Create a function for writing to SQL data to csv mg24 4 1,206 Oct-01-2022, 04:30 AM
Last Post: mg24
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,406 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,031 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,544 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Need Help writing data into Excel format ajitnayak87 8 2,568 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  Fastest Way of Writing/Reading Data JamesA 1 2,213 Jul-27-2021, 03:52 PM
Last Post: Larz60+
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,352 Mar-29-2021, 09:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020