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

  • SEARCH
  • Home
  • 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 7580671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:58:31+00:00 2026-05-30T17:58:31+00:00

Python beginner here. I am using the matplotlib library to make graphs from tab

  • 0

Python beginner here. I am using the matplotlib library to make graphs from tab delimited text files. I want my script to be flexible, so that it can take different types of data file and turn them into graphs. The key issue I have is that different text files have different numbers of header lines before the data begins. I would like to have a way for Python to figure out how many lines the header is, then remove them.

I think this could be done in two ways:

1) Count the most frequently occuring number of columns/elements per line in file, as most lines will be columns containing the data of interest. Then with a for loop, remove all lines that do not contain this number of columns.

2) Count number of columns/elements in last row of file, then remove any rows not matching this length. As in all the files I use the last row is also the data, this would work too.

If anyone can show me a short way to do this in python to integrate into my script that would really help a lot.

Many thanks,

Rubal

  • 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-05-30T17:58:33+00:00Added an answer on May 30, 2026 at 5:58 pm

    1)

    # lines = lines parsed out of file
    line_store = {}
    for line in lines:
        tokens = line.split('\t')
        if len(tokens) in line_store:
            line_store[len(tokens)].append(line)
        else:
            line_store[len(tokens)] = [line]
    most = []
    for line_group in line_store.values():
        if len(line_group) > len(most):
            most = line_group
    

    most will end up being the list you want

    2)

    # lines = lines parsed out of file
    tokens_in_last_line = len(lines[-1].split('\t'))
    lines_with_correct_number_of_tokens = []
    for line in lines[:-2]:
        if len(line.split('\t')) == tokens_in_last_line
            lines_with_correct_number_of_tokens.append(line)
    lines_with_correct_number_of_tokens.append(lines[-1])
    

    `lines_with_correct_number_of_tokens’ will have all your lines with the same number of tokens as the last line in the file.

    Both these solutions have major flaws though. (1) will choke if you have a header with the same number of tokens as the content rows, or if the header rows outnumber the content rows. (2) will choke if you have a footer line, or if the last line is blank, or, again, if your header rows have the same number of tokens as your content rows. I think you should see if you could come up with a more elegant solution.

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

Sidebar

Related Questions

beginner to python here. I have 2 nested lists that I want to merge:
I am a beginner programmer, using python on Mac. I created a function as
I came here to ask a simple, very beginner question regarding python. I just
I'm trying to get simple code working, unfortunately I'm a python beginner. My script
I'm currently learning python from a book called 'Python for the absolute beginner (third
Here's my background: Decent experience with PHP/MySql. Beginner's experience with OOP Why I want
I'm an absolute beginner using IDLE (Python 2.6.4) to learn the basics. I recently
I have the following script that is taken from a python game development book.
I'm python beginner: how can I iterate over csv files in one directory and
Beginner programmer here. So bear with me. I have a simple python program. print

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.