0

I can't quite figure out what is making this script of mine hang (Ie. Whole spyder stopped working). Is it something to do with my code or that some of my Spyder/python files are corrupted?

#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

Perhaps a keen eye can spot the problem if it truly is a code error. I can't seem to pick up any.

Ang Jit Wei Aaron
  • 389
  • 1
  • 3
  • 19

1 Answers1

0

Maybe there

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

The file name ends with '.xls', so it may not be a CSV file. Afterwards, you try to read it like a text file.

Sci Prog
  • 2,651
  • 1
  • 10
  • 18
  • Oh right. I wasn't aware that excel files can't be read. It worked now after changing it to .csv. Thanks! – Ang Jit Wei Aaron Feb 24 '16 at 06:18
  • They cannot be read as text files (as CSV files are), but you can search for python modules that allow you to. Take a look at http://stackoverflow.com/questions/2942889/reading-parsing-excel-xls-files-with-python – Sci Prog Feb 25 '16 at 00:21