pandas to_csv创建空的未指定文件 [英] pandas to_csv creates empty, unspecified file

查看:670
本文介绍了pandas to_csv创建空的未指定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般是python和编程的新手,所以这可能是一个愚蠢的问题.

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

我目前正在基于现有脚本和同事的教程,使用PsychoPy v1.90.2独立版本python 2.7(编码器版本)进行心理学实验.它可以在我的计算机(Mac)和Windows 10计算机上完美运行,但是在使用Windows7的计算机上,它不会将输出(即参与者的响应)保存到csv文件中.将创建一个空文件,没有任何文件规范,但文件名正确且位于正确的位置.

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.

  • 我可以正确导入基础的csv文件以创建刺激
  • 如果我包含print df,我可以在控制台中看到数据框,因此数据至少在临时位置存在
  • 我的系统(Mac)是德语,但Windows 10和7计算机设置为法语
  • 完全没有错误消息.
  • 我以前已经成功地在(比利时)法国Mac上完成了此实验.

最后应该是什么:一个csv文件,其中包含df最初包含的所有信息,以及包含参与者编号的参与者"列和包含参与者执行的按键的"ans"列.

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.

我尝试过的事情:

  • 不同版本的Psychopy,但使用python 3的版本除外
  • 我怀疑不同键盘布局的编码问题b/c,因此我进行了更改,但没有更改(并且在比利时法语计算机上不是问题)
  • 此处提出的解决方案:更改Python的默认编码?
  • 文件名中没有特殊字符,并非所有读入的csv文件中都有特殊字符,但它们均未保存.
  • 在计算机上使用的管理员权限
  • 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

我知道使用"experimentHandler"比纯"熊猫有一种更好的保存数据的方法,但是由于它以前工作过,所以到目前为止,我的知识仅限于我的同事对精神病的工作方式,因此我保留了该方法.我还有其他兼容性问题需要解决,但考虑到我目前所面临的时间压力,恐怕这是沉没的情况……

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!

推荐答案

脚本终止时,将写入cv数据(刷新").该问题很可能是由于Windows 7中特定于操作系统的原因而挂起的.我之前已经看到过此问题,并通过两种不同的方式解决了该问题:

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:

将此添加到脚本末尾:

w.close()
core.quit()

在脚本终止之前关闭csv文件对象

从答案中复制此处:用以下内容替换df.to_csv行,其中文件对象在此之前明确关闭脚本终止.

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()

这篇关于pandas to_csv创建空的未指定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆