Skip to content

Instantly share code, notes, and snippets.

@nrrb
Created November 26, 2012 20:32
Show Gist options
  • Save nrrb/4150433 to your computer and use it in GitHub Desktop.
Save nrrb/4150433 to your computer and use it in GitHub Desktop.
Reformat Qualtrics output to an edgelist suitable for use with UCINET
import csv
with open('c:/users/nick/downloads/Shalinee_Survey_for_Kineo.csv', 'rb') as f:
rows = list(csv.DictReader(f))
responses = []
# Skipping the first row, it's effectively the key from column headers to
# question text
for row in rows[1:]:
# V5 is the heading for the column containing the email address
respondent_email = row['V5']
for column_header, value in row.iteritems():
# Column headers look like Q1_1, Q1_10, Q1_11, etc
question_number = column_header.split('_')[0]
if question_number in ['Q1', 'Q2', 'Q3', 'Q4']:
# Need to extract the name from a question text like the following:
# Q1_1:
# Informal Learning Network and Features / / / Considering the “unmediated”
# and “unprompted” nature of i...-Adrian Day
# Q2_x1:
# On a scale of 1 to 5 (with 1 being the lowest and 5 being the highest),
# how motivated are you to inf...-Adrian Day
# Q3_x1:
# On / a scale of 1 to 5 (with 1 being the lowest and 5 being the highest),
# how motivated are you to inf...-Adrian Day
# Q4_1:
# On / a scale of 1 to 5 (with 1 being the lowest and 5 being the highest),
# how motivated are you to inf...-Adrian Day
target_name = rows[0][column_header].split('.-')[-1]
# Remove the unprintable characters that crept in
target_name = target_name.replace('\xa0', '').replace('\xc2', '')
# Remove double spaces and then replace spaces by underscores
target_name = target_name.replace(' ', ' ').replace(' ', '_')
# A non-response is an empty string, we'll represent that with a zero since
# that's unambiguous
if value == '':
value = 0
else:
value = int(value)
responses.append(dict(question=question_number,
respondent_email=respondent_email,
target_name=target_name,
value=value))
with open('c:/users/nick/downloads/Shalinee_edge_lists.csv', 'wb') as output_file:
dw = csv.DictWriter(output_file, fieldnames=[ 'question',
'respondent_email',
'target_name',
'value'])
dw.writeheader()
dw.writerows(responses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment