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 8908507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:11:50+00:00 2026-06-15T03:11:50+00:00

I want to write a script to process some data files. The data files

  • 0

I want to write a script to process some data files. The data files are just ascii text with columns of data, here is a simple example…

The first column is an ID number, in this case from 1 to 3. The second column is a value of interest. (The actual files I’m using have many more IDs and values, but let’s keep it simple here).

data.txt contents:

1 5
1 4
1 10
1 19
2 15
2 18
2 20
2 21
3 50
3 52
3 55
3 70

I want to iterate over the data and extract the values for each ID, and process them, i.e. get all values for ID 1 and do something with them, then get all values for ID 2 etc.

So I can write this in python.

#!/usr/bin/env python

def processValues(values):
  print "Will do something with data here: ", values

f = open('data.txt', 'r')
datalines = f.readlines()
f.close()

currentID = 0
first = True

for line in datalines:
    fields = line.split()

    # if we've moved onto a new ID,
    # then process the values we've collected so far
    if (fields[0] != currentID):

        # but if this is our first iteration, then
        # we just need to initialise our ID variable
        if (not first):
            processValues(values) # do something useful

        currentID = fields[0]
        values = []
        first = False

    values.append(fields[1])

processValues(values) # do something with the last values

The problem I have is that processValues() must be called again at the end. So this requires code duplication, and means that I might one day write a script like this and forget to put the extra processValues() at the end, and therefore miss the last ID. It also requires storing whether it is our ‘first’ iteration, which is annoying.

Is there anyway to do this without having two function calls to processValues() (one inside the loop for each new ID, one after the loop for the last ID)?

The only way I can think of is by storing the line number and checking in the loop if we’re at the last line. But it seems that removes the point of the ‘foreach’ style processing where we store the line itself, and not the index or the total number of lines. This would also apply to other scripting languages like perl, where it would be common to iterate over lines with while(<FILE>) and not have an idea of the number of lines remaining. Is it always necessary to write the function call again at the end?

  • 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-15T03:11:52+00:00Added an answer on June 15, 2026 at 3:11 am

    You want to look at itertools.groupby if all occurrences of a key are contigious – a basic example…

    from itertools import groupby
    from operator import itemgetter
    
    with open('somefile.txt') as fin:
        lines = ( line.split() for line in fin )
        for key, values in groupby(lines, itemgetter(0)):
            print 'Key', key, 'has values'
            for value in values:
                print value
    

    Alternatively – you can also look at using a collections.defaultdict with a list as the default.

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

Sidebar

Related Questions

I want to write a very simple script , which takes a process name
I want to write a Scala script to recursively process all files in a
I wrote a python script to process some data from CSV files. The script
I want to write simple script to copy/backup directory then remove on server startup.
I want to write some python package installing script in Python into virtualenv. I
I have 5 processes p1,p2,...,p5 where I want to write some data to stdin
I want to dump stack when some event happened, so I write a script
I want to write a bash script that (recursively) processes all files of a
I want to write a script for linux, that will first copy a movie/series
I want to write a script that simulates a proccess being interrupted for some

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.