Spyder在读取文件/运行csv函数时挂起

时间:2016-02-24 03:33:31

标签: python spyder

我无法弄清楚是什么让我的这个剧本挂起(即整个Spyder停止工作)。是与我的代码有关还是我的某些Spyder / python文件已损坏?

#Import relevant modules 
import os
import numpy as np
import csv

C1adaptS = 0
CN2adaptS = 0
N3adaptS = 0
NS4adaptS = 0
S5adaptS = 0
C1adaptC = 0
CN2adaptC = 0
N3adaptC = 0
NS4adaptC = 0
S5adaptC = 0

#Paste filename to be analysised here (The .csv file)
filename = 'PartID_WK_plottest.xls'

#Change directory to file's location
os.chdir('C:\Users\Aaron\Desktop\Experiments\Dotty_Simplified_(FINAL)')

loadcsv = open(filename, 'r')
for line in loadcsv.readlines():
    line = line.strip()
    #Split the words within the line
    trialnum, stimulus, stimulus_location, response, more_sin_like, sin_adapt = line.split(',')
    if stimulus == 'C1' and more_sin_like == 1 and sin_adapt == 1:
        C1adaptS += 1
    elif stimulus == 'CN2' and more_sin_like == 1 and sin_adapt == 1:
        CN2adaptS += 1
    elif stimulus == 'N3' and more_sin_like == 1 and sin_adapt == 1:
        N3adaptS += 1
    elif stimulus == 'NS4' and more_sin_like == 1 and sin_adapt == 1:
        NS4adaptS += 1
    elif stimulus == 'S5' and more_sin_like == 1 and sin_adapt == 1:
        S5adaptS += 1
    elif stimulus == 'C1' and more_sin_like == 1 and sin_adapt == 0:
        C1adaptC += 1
    elif stimulus == 'CN2' and more_sin_like == 1 and sin_adapt == 0:
        CN2adaptC += 1
    elif stimulus == 'N3' and more_sin_like == 1 and sin_adapt == 0:
        N3adaptC += 1
    elif stimulus == 'NS4' and more_sin_like == 1 and sin_adapt == 0:
        NS4adaptC += 1
    elif stimulus == 'S5' and more_sin_like == 1 and sin_adapt == 0:
        S5adaptC += 1

print C1adaptS
print CN2adaptS
print N3adaptS
print NS4adaptS
print S5adaptS
print C1adaptC
print CN2adaptC
print N3adaptC
print NS4adaptC
print S5adaptC

如果真的是代码错误,也许敏锐的眼睛可以发现问题。我似乎无法接受任何。

1 个答案:

答案 0 :(得分:0)

也许有

#Paste filename to be analysed here (The .csv file)
filename = 'PartID_WK_plottest.xls'

文件名以“.xls”结尾,因此它可能不是CSV文件。之后,您尝试像文本文件一样阅读它。