An Odd Little Script

James Stroud jstroud at mbi.ucla.edu
Wed Mar 9 17:51:42 EST 2005


This might be hack-ish, more elegant solutions are likely to exist, but this 
is the general idea:

LINEFEED = "\n"  # or whatever
EOR = "?"        # or whatever

def fixEOR(filename):
  f = open(filename,"r+")
  byte = True
  # loops through whole file
  while True:
    # loops through record
    while byte and (byte != EOR):
      byte=f.read(1)
    if not byte:
      break
    f.seek(-1,1)            # backup 1
    byte = LINEFEED
    f.write(byte)           # incidentally, will put LINEFEED at EOF
    f.flush()
  f.close()



On Wednesday 09 March 2005 02:06 pm, Greg Lindstrom wrote:
> Hello-
>
> I have a task which -- dare I say -- would be easy in <asbestos_undies>
> Perl </asbestos_undies> but would rather do in Python (our primary
> language at Novasys).  I have a file with varying length records.  All
> but the first record, that is; it's always 107 bytes long.  What I would
> like to do is strip out all linefeeds from the file, read the character
> in position 107 (the end of segment delimiter) and then replace all of
> the end of segment characters with linefeeds, making a file where each
> segment is on its own line.  Currently, some vendors supply files with
> linefeeds, others don't, and some split the file every 80 bytes.  In
> Perl I would operate on the file in place and be on my way.  The files
> can be quite large, so I'd rather not be making extra copies unless it's
> absolutely essential/required.
>
> I turn to the collective wisdom/trickery of the list to point me in the
> right direction.  How can I perform the above task while keeping my sanity?
>
> Thanks!
> --greg
> --
> Greg Lindstrom               501 975.4859
> Computer Programmer          greg.lindstrom at novasyshealth.com
> NovaSys Health
> Little Rock, Arkansas
>
> "We are the music makers, and we are the dreamers of dreams."  W.W.

-- 
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095



More information about the Python-list mailing list