License Public Domain
Lines 59
Keywords
csv (2)
Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users
Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

CSV data analysis Atom Feed 0

In Brief Some python code for analyzing some data for Janeth... not really useful for YOU, but you can look at it as an example of using the Python CSV library.
# 's
 1#!/usr/bin/python
2
3import csv
4
5DATA_FILENAME = 'jdata-new.csv'
6
7# Open the data
8reader = csv.reader(open(DATA_FILENAME))
9# Open the data to write
10#writer = csv.writer(open('joutput-new.csv'))
11
12
13# Subject [1]
14# Session [2]
15# WordPool [27]
16# Response [24]
17
18max_sessions = 4
19
20word_pool_ids = []
21response_types = ['1', '2', '3', 'z', 'x', 'c']
22
23responses = {'z':0, 'x':0, 'c':0,
24 '1':0, '2':0, '3':0}
25subjects = {}
26
27
28# Skip the headers
29reader.next()
30
31for row in reader:
32 if len(row) < 28:
33# print 'Skipping invalid row ', row
34 continue
35
36 subject_id = row[1].strip()
37 session = int(row[2]) - 1
38 word_pool_id = row[27].strip()
39 response = row[24].strip()
40
41 if word_pool_id not in word_pool_ids:
42 word_pool_ids.append(word_pool_id)
43
44 # Initialize the subject
45 if not subjects.has_key(subject_id):
46 # List of wordpool dictionaries
47 subjects[subject_id] = [{} for i in range(max_sessions)]
48
49 # Initialize a wordpool
50 if not subjects[subject_id][session].has_key(word_pool_id):
51 subjects[subject_id][session][word_pool_id] = responses.copy()
52
53 subjects[subject_id][session][word_pool_id][response] += 1
54# Output the data
55
56print 'Subject, Session,',
57
58for i in word_pool_ids:
59 for j in response_types:
60 print '%s "%s",' % (i, j),
61print
62
63# Iterate over the subjects
64for sub_id, sessions in subjects.iteritems():
65 # Iterate over each session
66 for s_id, session in enumerate(sessions):
67 print '%s, %s,' % (sub_id, s_id + 1),
68
69 for i in word_pool_ids:
70 for j in response_types:
71 if session.has_key(i):
72 print session[i][j], ',',
73 print

Some python code for analyzing some data for Janeth... not really useful for YOU, but you can look at it as an example of using the Python CSV library.