Skip to content

Instantly share code, notes, and snippets.

@haya14busa
Created November 19, 2013 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haya14busa/7540886 to your computer and use it in GitHub Desktop.
Save haya14busa/7540886 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
import os
import csv
def readFile(filename, lines):
f = open(filename, 'r')
try:
if lines == 1:
return f.readlines()
else:
return f.read()
finally:
f.close()
def writeFile(filename, content):
f = open(filename, 'w')
try:
f.write(content)
finally:
f.close()
def correctCSV(filename,newfilename):
results = csv.reader(open(filename,'rb'), delimiter='\t')
i = 0
back = 0
corrected = []
for row in results:
if i < 7:
i += 1
corrected.append(row)
continue
# break and instruction
if row[4] == 'break' or row[4].find('instruction') >= 0:
# Reset
back = 0
corrected.append(row)
continue
# row[10] -> reaction time
if int(row[10]) < back:
#print row[10]
# now time is back!
back = int(row[10])
else:
# substruct back from time
tmp = int(row[10])
row[10] = str(int(row[10]) - back)
back = tmp
corrected.append(row)
# Write
writecsv = csv.writer(open(newfilename,'wb'), delimiter='\t')
writecsv.writerows(corrected)
def getArgs():
return sys.argv[1:]
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
if __name__ == '__main__':
CorrectDir = 'corrected'
for filename in getArgs():
#midpath = '' if filename.rfind('/') < 0 else filename[:filename.rfind('/')]
#os.makedirs(CorrectDir+'/'+midpath)
ensure_dir(CorrectDir+'/'+filename)
correctCSV(filename,CorrectDir+'/'+filename)
#print getArgs()
print 'Successfully created under corrected directory'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment