Ben
Ben

Reputation: 21

How to perform multiple functions on a text file

I am very new to python, and having gone through some exercises I thought it would be a good idea to write my first program! (Perhaps an overly ambitious goal).

I have a textfile and want to extract several pieces of information from it. The text file is an output file from stimulus delivery software, and is arranged in columns and lines.

I want the program to record how many times a certain string is present,and then also record two other pieces of information from the surrounding text in the file.

For example, from the following text:

RXXXX   9   Picture CATCH_QUAIL_600_5_3000_2590_278 1026069 7999    2   3000    3   7900    2960    other   0

RXXX    9   Picture poststim_fixation   1029236 0   1   25997   2   0   25900   other   0

RXXXX   9   Response    115 1036879 7643    1

I want it to report that there was a response, on trial number 9 and that the response was for the Picture 'CATCH_QUAIL'.

I think the best way to do this is for the program to find the string 'Response' and then extract the information from two lines above and from one column to the left.

So, this is what I have so far (I am sorry its pathetic):

    x= open('file')
    y= x.read()
    y.split()
    l= y.splitlines()

Then I know I need to make some loops which will cycle through the file, and if it finds the 'response' string then move to a new loop which will record the information I want- unfortunately I have no idea how to do this.

If at all possible, I really want to learn how to do this so if you could give me some hints rather than a complete code it would be great.

Having looked through some of the other questions on here, I am sorry if this is far below the caliber of question that you would expect but I am not sure where else to turn!

Thanks,

Ben

Upvotes: 1

Views: 562

Answers (3)

Ben
Ben

Reputation: 21

Thanks to you all for the help. I think it is clear that there are many ways one could go about doing this, below is the code that I arrived at- it is probably not the neatest, but I find it fairly transparent and easy to manipulate.

x= open('file')
logs=x.readlines()
iLine = logs[6]

log_enumerater = enumerate(logs)
for iLine in log_enumerater:
    if iLine[1].find('CATCH') != -1: 
            Event=iLine[1].split('\t')[3]
            word=Event.split('_')[1]
            t0=int(iLine[1].split('\t')[4])
            print iLine[1].split('\t')[3].split('_')[1], iLine[0]
            print 'Catch in line ', iLine[0] 
            myLine = int(iLine[0])+2
            print 'Response in Line', myLine
    if iLine[1].find('Response') != -1:
            t1= int(iLine[1].split('\t')[4])
            ResponseTime= t1-t0  
            print ResponseTime

I have been using it to get the trial numbers and now have adapted it slightly for getting response times.

Thank you again for all your help,

Ben

Upvotes: 1

martineau
martineau

Reputation: 123423

I would read the information from the file and put it in a list-of-lists like this:

data = []
with open('textfile.txt') as inputfile:
    for line in inputfile.read().splitlines():
        if line:
            data.append(line.split())
print data

Result:

[['RXXXX', '9', 'Picture', 'CATCH_QUAIL_600_5_3000_2590_278', '1026069', '7999', '2', '3000', '3', '7900', '2960', 'other', '0'],
 ['RXXX', '9', 'Picture', 'poststim_fixation', '1029236', '0', '1', '25997', '2', '0', '25900', 'other', '0'],
 ['RXXXX', '9', 'Response', '115', '1036879', '7643', '1']]

That way you can access the contents of any column of any non-blank line, and it would allow you to look backwards at previous lines when necessary as shown below:

for i, line in enumerate(data):
    if line[2] == 'Response':
        print 'Got response on trial numder', line[1]
        if data[i-2][2] == 'Picture' and data[i-2][3].startswith('CATCH_QUAIL'):
            print '  The response was for the picture', data[i-2][3]

Output:

Got response on trial numder 9
  The response was for the picture CATCH_QUAIL_600_5_3000_2590_278

Upvotes: 0

msturdy
msturdy

Reputation: 10794

Generally the python docs are a good place to start, check out the section on reading/writing files. Mentioned there is a very useful pattern for working with files in Python:

#!/usr/bin/env python
with open("file", "r") as my_file:
  for i, my_line in enumerate(my_file):
    print i, my_line

This will open the file (my_file) for reading (option "r"), and then just print each line (my_line) and its position (i) for you.

The second useful pattern here is the enumerate() in the second line of the script This takes a list, and for each item gives you back the item, and it's "index" or position in the list. So, for example:

for index, item in enumerate(["a", "b", "c"]):
  print index, ":", item

gives:

0 : a
1 : b
2 : c

Ok, now you might want to consider using the in keyword to search for the substring "Response" within the line:

if "Response" in my_line:
  print "found Response in line %s!" % i

try running the whole thing, and see what you get

with open("py-test.txt", "r") as my_file:
  for i,my_line in enumerate(my_file):
    print i, my_line
    if "Response" in my_line:
      print "found Response in line %s!" % i

so, now you're finding the line you want and getting its position in the file, you'll just need a way to access a specific line in the file, and access the information you need from that line..

Upvotes: 0

Related Questions