def _get_counts(result, question_id): counts = OrderedDict() for row in result: if not row['country'] in counts.keys(): counts[row['country']] = initialize_counts(question_id) counts[row["country"]][row["response"]] += 1 return counts
def summarize_question(question_id): """ Summarize responses for a given question ID """ question, result = query(question_id) print '{0}: {1}'.format(question_id, question['label']) counts = OrderedDict() for row in result: if not row['country'] in counts.keys(): counts[row['country']] = initialize_counts(question_id) counts[row["country"]][row["response"]] += 1 output = [] for country, values in counts.items(): output_row = OrderedDict((('country', country),)) total = 0 for label, value in values.items(): total += int(value) output_row['total_responses'] = total for label, value in values.items(): output_row[label] = value pct_label = '{0} pct'.format(label.encode('ascii', 'ignore').decode('ascii')) output_row[pct_label] = float(value) / total output.append(output_row) dataset.freeze(output, format='csv', filename='output/{0}.csv'.format(question_id))
def summarize_question(question_id): """ Summarize responses for a given question ID """ question, result = query(question_id) print '{0}: {1}'.format(question_id, question['label']) counts = OrderedDict() for row in result: if not row['country'] in counts.keys(): counts[row['country']] = initialize_counts(question_id) counts[row["country"]][row["response"]] += 1 output = [] for country, values in counts.items(): output_row = OrderedDict((('country', country), )) total = 0 for label, value in values.items(): total += int(value) output_row['total_responses'] = total for label, value in values.items(): output_row[label] = value pct_label = '{0} pct'.format( label.encode('ascii', 'ignore').decode('ascii')) output_row[pct_label] = float(value) / total output.append(output_row) dataset.freeze(output, format='csv', filename='output/{0}.csv'.format(question_id))