Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:21:54+00:00 2026-06-03T00:21:54+00:00

I know this is simple, but I’m a new user to Python so I’m

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:21:56+00:00Added an answer on June 3, 2026 at 12:21 am

    The issue here is that you are using readlines() twice, which means that the data is read the first time, then nothing is left the second time.

    You can iterate directly over the file without using readlines() – in fact, this is the better way, as it doesn’t read the whole file in at once.

    While you could do what you are trying to do by using str.split() as you have, the better option is to use the csv module, which is designed for the task.

    import csv
    
    with open("addressbook1.txt") as input, open("college_age.txt", "w") as college, open("adult_age.txt", "w") as adult:
       reader = csv.DictReader(input, dialect="excel-tab")
       fieldnames = reader.fieldnames
       writer_college = csv.DictWriter(college, fieldnames, dialect="excel-tab")
       writer_adult = csv.DictWriter(adult, fieldnames, dialect="excel-tab")
       writer_college.writeheader()
       writer_adult.writeheader()
       for row in reader:
           if int(row["Age"]) < 23:
              writer_college.writerow(row)
           else:
              writer_adult.writerow(row)
    

    So what are we doing here? First of all we use the with statement for opening files. It’s not only more pythonic and readable but handles closing for you, even when exceptions occur.

    Next we create a DictReader that reads rows from the file as dictionaries, automatically using the first row as the field names. We then make writers to write back to our split files, and write the headers in. Using the DictReader is a matter of preference. It’s generally used more where you access the data a lot (and when you don’t know the order of the columns), but it makes the code nice a readable here. You could, however, just use a standard csv.reader().

    Next we loop through the rows in the file, checking the age (which we convert to an int so we can do a numerical comparison) to know what file to write to. The with statement closes out files for us.

    For multiple input files:

    import csv
    
    fieldnames = ["Name", "Date", "Age", "Sex", "Color"]
    filenames = ["addressbook1.txt", "addressbook2.txt", ...]
    
    with open("college_age.txt", "w") as college, open("adult_age.txt", "w") as adult:
       writer_college = csv.DictWriter(college, fieldnames, dialect="excel-tab")
       writer_adult = csv.DictWriter(adult, fieldnames, dialect="excel-tab")
       writer_college.writeheader()
       writer_adult.writeheader()
       for filename in filenames:
           with open(filename, "r") as input:
               reader = csv.DictReader(input, dialect="excel-tab")
               for row in reader:
                   if int(row["Age"]) < 23:
                      writer_college.writerow(row)
                   else:
                      writer_adult.writerow(row)
    

    We just add a loop in to work over multiple files. Please note that I also added a list of field names. Before I just used the fields and order from the file, but as we have multiple files, I figured it would be more sensible to do that here. An alternative would be to use the first file to get the field names.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a simple question for someone out there, but I have
I know this is simple but I can't get it working! I have no
I know this is a simple question but it's aggravating me. If I have
I know this should be simple but I can't make it work. I have
Hi, I know this should be really simple but I am just too new
I know this is a very simple question but I seem to be having
I know this should be a simple task but I'm having problems selecting a
I know this is a simple question but I have never done this before
I know this should be simple but I am having a difficult time navigating
i know this is very simple question but i am new on this so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.