-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_eprime_header.py
44 lines (35 loc) · 1.26 KB
/
parse_eprime_header.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
"""
Functions to extract the headers of all the subjects
@author: Mehdi Rahim
"""
import os, glob
BASE_DIR = os.path.join('/', 'shfj', 'Ppsypim', 'PSYDAT', 'Stats', 'eprime')
# Header Parsing Function
def parse_header_eprime(filename):
""" returns a dictionary of values in the header of the filename
"""
header = {}
with open(filename, 'rU') as f:
lines = [x.strip() for x in f.read().split('\n')]
for line in lines:
if line == "*** Header Start ***":
continue
if line == "*** Header End ***":
return header
fields = line.split(": ")
if len(fields) == 2:
header[fields[0]] = fields[1]
# File ID Parsing Function
def parse_file_id_eprime(filename):
""" returns the file_id of from the filename
"""
return filename.split('-')[1]
##############################################################################
# parse header for all the subjects
file_list = glob.glob(os.path.join(BASE_DIR, "*.txt"))
for fn in file_list:
h, fname = os.path.split(fn)
head = parse_header_eprime(fn)
print head['SessionDate'], '-', head['SessionTime']
print head['Subject'].strip(), '-', parse_file_id_eprime(fn), '-', fname