Jissel Anaya Jissel Anaya - 1 year ago 80
Python Question

How can I adjust my data in a text file

I'm trying to use Python to help me organize my data. Specifically, I'm trying to figure out how I can adjust onset times, and separate correct and incorrect responses using Python.

For example my experiment results for each subject is stored in a text file (sub001.txt).here is one subject file.

The 1st column corresponds to the onset time(in seconds).

The 2nd column corresponds to the response time (in

The 3rd column corresponds to whether or not the response was
correct (0: incorrect, 1: correct).

I am trying to write a program to read this file, then do the following:

a. Subtract 10s from all onset times.

b. Separate the files associated with correct and incorrect responses so that I can run statistics on those separately.

So I will have 2 files for one subject (sub001_correct.txt and sub001_incorrect.txt). Each file should contain two columns. The first column should contain the corrected onset
times (from a. above). The second column should contain the
response time.

I tried looking at other questions and answers but haven't had much luck.

Answer Source

The following code does what you want:

f = open('sub001.txt', 'r')
f_correct = open('correct001.txt', 'w')
f_incorrect = open('incorrect001.txt', 'w')

for line in f:
    parts = line.strip().split(' ')
    onset = int(parts[0])
    response = parts[2]
    correctness = parts[4]

    corrected_onset = onset - 10
    if correctness == '1':
        new_entry = str(corrected_onset) + ' ' + response + '\n'
        new_entry = str(corrected_onset) + ' ' + response + '\n'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download