[CentralOH] Weird issue reading a Mainframe file with Python

Andrew Kubera akubera at kent.edu
Wed May 20 17:28:59 CEST 2015

It looks like read() is returning a string, then the for loop is actually looping through that string one character at a time.

If each of the records are on one line, you can simply iterate over the file:   for line in fp: ...

If not, you should turn your loop into:

while True:
    line = fp.read(26)
    if len(line) < 26: break

Or be fancy with a generator (I assume that’s in python2.4?):

def next_record(file):
    while True:
    line = fp.read(26)
    if len(line) < 26: raise StopIteration
    yield line

for record in next_record(fp):
- Andrew Kubera

> On May 20, 2015, at 11:04 AM, Joshua Kramer <joskra42.list at gmail.com> wrote:
> Hello all!
> I have a rather bizarre issue reading files that came from an IBM
> Mainframe.  I am on a Linux server, FTP'ing the files from the
> Mainframe, and running a Python script.  It seems like the Python
> script always reads just one byte at a time from the file!  For
> example, if you have a file with records like this (this is two
> records, each one being of length 26):
> 00423SMITH   JOHN  126400100423SMITH   ROBERT1264001
> If you open this file with any text editor on Linux, it appears as it is there.
> If I do this in Python:
> fp = open("FILE.DAT", "rb")
> for line in fp.read(26):
>    print "Record is: " + line
> Then what I will get is this:
> Record is: 0
> Record is: 0
> Record is: 4
> Record Is: 2
> Record Is: 3
> ...etcetera.
> I wrote the same program in C, and I got the results I expected- not
> these results.  So I have reason to believe that the file is good.
> I am using Python 2.4.3.  (Unfortunately, stuck on an ancient RedHat server...)
> What am I missing here?
> Thanks!
> -JK
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20150520/71afd01e/attachment-0001.html>

More information about the CentralOH mailing list