Seite 1 von 1

Großen binaeren File lesen

Verfasst: Dienstag 4. Oktober 2005, 13:39
von mbierenfeld
Hallo

ich habe einen 2Gigabyte großen Binärfile. Den würde ich gerne Stück für Stück einlesen von EBCDIC nach ASCII konvertieren und wegschreiben.

Hat jemand schnell im Kopf wie das "binäre" Gegenstück von file.readlines () ausschaut. Ein kurzes Code Snipplet wäre super.

Grüße

Michael

Verfasst: Dienstag 4. Oktober 2005, 14:02
von Gast
Das muss aber doch einfacher gehen als so. ich stehe irgendwie auf dem schlauch

Code: Alles auswählen

def convert (input, output):

    err = None
    blocksize = 1000
    totalread = 0

    inputfile = open (input, "r")
    
    while 1:
    
        content = inputfile.read (blocksize)
        
        bytesread = len (content)
        totalread += bytesread
        
        if bytesread > 0:
            print "Hallo habe [%d] gelesen" % (totalread)
            print "Hier koennte ich nun convertieren"

        if bytesread < blocksize:
            break
    
    inputfile.close ()
    
    return err
Edit (Leonidas): Code in Python-Tags gesetzt.

Verfasst: Dienstag 4. Oktober 2005, 16:06
von Leonidas
Ich hätts ja so gemacht:

Code: Alles auswählen

#!/usr/bin/env python
# -*- encoding: latin-1 -*- 

def convert(input, output):
    blocksize = 1000
    inputfile = file(input, "rb")
   
    while True:
        content = inputfile.read(blocksize)
        if content == '':
            break
        
        print "Hier koennte ich nun convertieren"

    inputfile.close()
   
    return None

convert('modindex.txt', None)