pandas to_csv creates empty, unspecified file

偶尔善良 提交于 2019-12-23 21:24:54

问题


I am new to python and programming in general, so this is probably a stupid question.

I am currently preparing a psychology experiment using PsychoPy v1.90.2 standalone, python 2.7, the coder version, based on both an existing script and a tutorial from a colleague. It works perfectly on my computer (Mac) and on a Windows 10 computer, but on a computer using Windows7, it does not save the output (i.e. the participant's responses) to a csv file. An empty file is created, without any file specification, but the correct file name and in the correct location.

  • I can correctly import the underlying csv file for stimulus creation
  • if I include print df, I can see the dataframe in the console, so the data exists somewhere, at least temporarily
  • My system (Mac) is german, but the Windows 10 and 7 computers are set to french
  • There is no error message at all.
  • I have successfully done this experiment on a (belgian) french Mac before.



from psychopy import visual, core, event, gui, info, data
import pandas as pd
import numpy as np
import os 
import random 
import sys
import time
GUI = gui.Dlg(title = "example")
GUI.addField('participant:')
GUI.show()
if GUI.OK:
    metadata = GUI.data
else:
    sys.exit('participant cancelled')
w = visual.Window([1000, 600], color='black', units='pix')
df = pd.DataFrame([[1, 2], [5, 3], [4, 6]], columns=['stim_a', 'stim_b'])
df['participant'] = int(metadata[0])
df['ans'] = ''
df = df.iloc[random.sample(df.index, len(df))]
df.index = range(len(df))
output_df = "S_" + str(df.participant) + "_df" + "_" + time.strftime("%Y-%m:%d_%Hh%M") + ".csv"
for j in range(df.shape[0]):
    event.clearEvents()
    stim_a = visual.TextStim(w, df.stim_a[j])
    stim_a.draw()
    w.flip()
    resp = event.waitKeys()
    df.ans[j] = resp
    print df
df.to_csv(output_df, index = False, header = True, sep = ',', encoding = 'utf-8')

What should be there in the end: a csv file containing all the information contained in df initially, plus the "participant" column containing the participant number and the "ans" column containing the key presses performed by the participant.

What I tried:

  • different versions of psychopy, except the one using python 3
  • I suspected an encoding problem b/c of the different keyboard layouts, so I changed that, but no change (and hadn't been a problem on the belgian french computers)
  • The solutions proposed here: Changing default encoding of Python?
  • There are no special characters in the file names, and not in all of the csv files read in, but they all aren't saved.
  • used admin privileges on the computer

I know there is a better way to save data than "pure" pandas by using the experimentHandler, but as it worked before, and my knowledge so far is limited to my colleague's way of working with psychopy, i kept that method. I had other compatibility issues which I managed to solve, and I am afraid given the time pressure I am currently in, it is a sunken-cost situation...

EDIT: the working example should now work, sorry for that!


回答1:


The csv data is written ("flushed") when the script terminates. The problem is likely that it hangs for OS-specific reasons in Windows 7. I have seen this before and solved it in two different ways:

Close psychopy before script terminates

Add this to the end of the script:

w.close()
core.quit()

Close csv file object before script terminates

Copied from an answer here: replace the df.to_csv line with the following where the file object is explicitly closed before the script terminates.

outfile = open(path+'filename.csv', 'wb')
df.to_csv(output_df, index = False, header = True, sep = ',', encoding = 'utf-8')
outfile.close()


来源:https://stackoverflow.com/questions/51044704/pandas-to-csv-creates-empty-unspecified-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!