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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:22:27+00:00 2026-05-26T11:22:27+00:00

I’ve got a little script which is not working nicely for me, hope you

  • 0

I’ve got a little script which is not working nicely for me, hope you can help and find the problem.

I have two starting files:
traveltimes: contains the lines I need, it’s a column file (every row has just a number). The lines I need are separated by a line which starts with 11 whitespaces

header lines: contains three header lines

output_file: I want to get 29 files (STA%s). What’s inside? Every file will contain the same header lines after which I want to append the group of lines contained in the traveltimes file (one different group of lines for every file). Every group of lines is made by 74307 rows (1 column)

So far this script creates 29 files with the same header lines but then it mixes up everything, I mean it writes something but it’s not what I want.

Any idea????

def make_station_files(traveltimes, header_lines):
"""Gives the STAxx.tgrid files required by loc3d"""
sta_counter = 1
with open (header_lines, 'r') as file_in:
    data = file_in.readlines()
    for i in range (29):
        with open ('STA%s' % (sta_counter), 'w') as output_files: 
            sta_counter += 1
            for i in data [0:3]:
                values = i.strip()                   
                output_files.write ("%s\n\t1\n" % (values))
                with open (traveltimes, 'r') as times_file:
                    #collector = []
                    for line in times_file:
                            if line.startswith ("            "):
                                break
                            output_files.write ("%s" % (line))
  • 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-26T11:22:28+00:00Added an answer on May 26, 2026 at 11:22 am

    Suggestion:

    • Read the header rows first. Make sure this works before proceeding. None of the rest of the code needs to be indented under this.
    • Consider writing a separate function to group the traveltimes file into a list of lists.
    • Once you have a working traveltimes reader and grouper, only then create a new STA file, print the headers to it, and then write the timegroups to it.

    Build your program up step-by-step, making sure it does what you expect at each step. Don’t try to do it all at once because then you won’t easily be able to track down where the issue lies.

    My quick edit of your script uses itertools.groupby() as a grouper. It is a little advanced because the grouping function is stateful and tracks it state in a mutable list:

    def make_station_files(traveltimes, header_lines):
        'Gives the STAxx.tgrid files required by loc3d'
    
        with open (header_lines, 'r') as f:
            headers = f.readlines()
    
        def station_counter(line, cnt=[1]):
            'Stateful station counter -- Keeps the count in a mutable list'
            if line.strip() == '':
                cnt[0] += 1
            return cnt[0]
    
        with open(traveltimes, 'r') as times_file:
            for station, group in groupby(times_file, station_counter):
                with open('STA%s' % (station), 'w') as output_file:
                    for header in headers[:3]:
                        output_file.write ('%s\n\t1\n' % (header.strip()))
                    for line in group:
                        if not line.startswith('           '):
                            output_file.write ('%s' % (line)) 
    

    This code is untested because I don’t have sample data. Hopefully, you’ll get the gist of it.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.