I know this is simple, but I'm a new user to Python so I'm having a bit of trouble here. I'm using Python 3 by the way.
I have multiple files that look something like this:
NAME DATE AGE SEX COLOR
Name Date Age Sex Color
Ray May 25.1 M Gray
Alex Apr 22.3 F Green
Ann Jun 15.7 F Blue
(Pretend this is tab delimited. I should add that the real file will have about 3,000 rows and 17-18 columns)
What I want to do is select all the rows which have a value in the age column which is less than 23.
In this example, the output would be:
Name Date Age Sex Color
Alex Apr 22.3 F Green
Ann Jun 15.7 F Blue
Here's what I tried to do:
f = open("addressbook1.txt",'r')
line = f.readlines()
file_data =[line.split("\t")]
f.close()
for name, date, age, sex, color in file_data:
if age in line_data < 23:
g = open("college_age.txt",'a')
g.write(line)
else:
h = open("adult_age.txt",'a')
h.write(line)
Now, ideally, I have 20-30 of these "addressbook" inputfiles and I wanted this script to loop through them all and add all the entries with an age under 23 to the same output file ("college_age.txt"). I really don't need to keep the other lines, but I didn't know what else to do with them.
This script, when I run it, generates an error.
AttributeError: 'list' object has no attribute 'split'
Then I change the third line to:
file_data=[line.split("\t") for line in f.readlines()]
And it no longer gives me an error, but simply does nothing at all. It just starts and then starts.
Any help? :) Remember I'm dumb with Python.
I should have added that my actual data has decimals and are not integers. I have edited the data above to reflect that.