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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:05:34+00:00 2026-05-27T06:05:34+00:00

I have written a python script to process a set of ASCII files within

  • 0

I have written a python script to process a set of ASCII files within a given dir. I wonder if there is a more concise and/or “pythonesque” way to do it, without loosing readability?

Python Code

import os
import fileinput
import glob
import string

indir='./'
outdir='./processed/'

for filename in glob.glob(indir+'*.asc'): # get a list of input ASCII files to be processed
    fin=open(indir+filename,'r')   # input file
    fout=open(outdir+filename,'w') # out: processed file

    lines = iter(fileinput.input([indir+filename])) # iterator over all lines in the input file
    fout.write(next(lines)) # just copy the first line (the header) to output

    for line in lines:
        val=iter(string.split(line,' '))
        fout.write('{0:6.2f}'.format(float(val.next()))), # first value in the line has it's own format
        for x in val: # iterate over the rest of the numbers in the line
            fout.write('{0:10.6f}'.format(float(val.next()))),  # the rest of the values in the line has a different format 
        fout.write('\n')

    fin.close()
    fout.close()

An example:

Input:

;;; This line is the header line
-5.0 1.090074154029272 1.0034662411357929 0.87336062116561186 0.78649408279093869 0.65599958665017222 0.4379879132749317 0.26310799350679176 0.087808018565486673
-4.9900000000000002 1.0890770415316042 1.0025480136545413 0.87256100700428996 0.78577373527626004 0.65539842673645277 0.43758616966566649 0.26286647978335914 0.087727357602906453
-4.9800000000000004 1.0880820021223023 1.0016316956763136 0.87176305623792771 0.78505488659611744 0.65479851808106115 0.43718526271594083 0.26262546925502467 0.087646864773454014
-4.9700000000000006 1.0870890372077564 1.0007172884938402 0.87096676998908273 0.78433753775986659 0.65419986152386733 0.4367851929843618 0.26238496225635727 0.087566540188423345
-4.9600000000000009 1.086098148170821 0.99980479337809591 0.87017214936140763 0.78362168975984026 0.65360245789061966 0.4363859610200459 0.26214495911617541 0.087486383957276398

Processed:

;;; This line is the header line
-5.00  1.003466  0.786494  0.437988  0.087808
-4.99  1.002548  0.785774  0.437586  0.087727
-4.98  1.001632  0.785055  0.437185  0.087647
-4.97  1.000717  0.784338  0.436785  0.087567
-4.96  0.999805  0.783622  0.436386  0.087486
  • 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-27T06:05:34+00:00Added an answer on May 27, 2026 at 6:05 am

    Other than a few minor changes, due to how Python has changed through time, this looks fine.

    You’re mixing two different styles of next(); the old way was it.next() and the new is next(it). You should use the string method split() instead of going through the string module (that module is there mostly for backwards compatibility to Python 1.x). There’s no need to use go through the almost useless “fileinput” module, since open file handle are also iterators (that module comes from a time before Python’s file handles were iterators.)

    Edit: As @codeape pointed out, glob() returns the full path. Your code would not have worked if indir was something other than “./”. I’ve changed the following to use the correct listdir/os.path.join solution. I’m also more familiar with the “%” string interpolation than string formatting.

    Here’s how I would write this in more idiomatic modern Python

    def reformat(fin, fout):
        fout.write(next(fin)) # just copy the first line (the header) to output
        for line in fin:
            fields = line.split(' ')
    
            # Make a format header specific to the number of fields
            fmt = '%6.2f' + ('%10.6f' * (len(fields)-1)) + '\n'
    
            fout.write(fmt % tuple(map(float, fields)))
    
    basenames = os.listdir(indir)  # get a list of input ASCII files to be processed
    for basename in basenames:
        input_filename = os.path.join(indir, basename)
        output_filename = os.path.join(outdir, basename)
        with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout:
            reformat(fin, fout)
    

    The Zen of Python is “There should be one– and preferably only one –obvious way to do it”. It’s interesting how you functions which, during the last 10+ years, was “obviously” the right solution, but are no longer. 🙂

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

Sidebar

Related Questions

I have written a python script which watches a directory for new subdirectories, and
I have written a python script with methods in it. Now I want to
I have a mechanize script written in python that fills out a web form
I have written a Python script and compiled it into a MS Windows EXE
I have written up a python script that allows a user to input a
Greetings. I have written a little python script that calls MySQL in a subprocess.
I have an installation script written in Python (in Linux) that runs as root
I have written a Python script that currently is designed to handle traditional CGI
I have written a Python script that checks a certain e-mail address and passes
I have written a Python script to download all of the xkcd comic images.

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.