Belinda Belinda - 1 month ago 5
Python Question

How can I organize my data to be in one line using Python?

Data description:
My data file ExpData.txt, included here, contains the experiment results involving multiple subjects.

1) The 1st column corresponds to 4-digit subject IDs

2) The 2nd column corresponds to response times. (The number of trials for each subject ranges from 15 to 25.)
Looks like this:

0001 81
0001 112
0001 125
0001 99
0001 120


Aim:
I need to be able to read this data file and re-format the data with one subject per line (to make it easier to analyze statistically). In other words, each line starts with a subject ID, followed by the response times for that subject, sorted in the ascending order and separated by a space.
Should look like this:

0001 80 81 . . . 120 125
0003 77 78 . . . 109 110 112 117 120 123
0005 78 78 . . . 120 120
-. . .


I need to save the reformatted data as a separate data file called ExpDataReformat.txt.

Any ideas on how to do this? or where to even start?

I started by trying to edit this code someone else shared on a post that appends and sorts states and state capitals. Needless to say, I kept getting syntax errors.

infile = open('TestData/StateCapitalList.txt','r')
stateData = infile.read()
infile.close()

dataLines = stateData.split('\n')

state = []
capital = []

for line in dataLines:

if line:
iState, iCapital = line.split(',')
state.append(iState)
capital.append(iCapital)

outfile = open('TestData/sCapitalList.txt','w')

capital.sort()

for iCap in capital:

outfile.write(iCap + '\n')

outfile.close()

Answer
from collections import defaultdict

result = defaultdict(list)

with open('ExpData.txt') as infile:
    for line in infile:
        key, value = line.split()
        result[key].append(int(value))

with open('out.txt', 'w') as outfile:
    for key, value_list in sorted(result.items()):
        outfile.write(' '.join([key] + map(str, sorted(value_list)))
                      + '\n')