Priyanka Iyer
Priyanka Iyer

Reputation: 185

reading a line from a file and splitting it - python

I have a file with just 1 line of data ( close to 3 MB ) I need to split it into smaller lines and write the output to a new file.

for eg :

sample.txt - file 434D012000100009362D00000000069E0F0007000000DA434D01030010010003008000000000000000000000009C434D01200010000 .... and so on

I want to split the whole line into smaller lines each of 23 bytes each. ie how do i get to write a python script to tell break after so many characters

Thanks.

Upvotes: 1

Views: 468

Answers (2)

Fredrik Pihl
Fredrik Pihl

Reputation: 45670

Use a function as a generator. I'm quite fond of the array-module.

def bytesfromfile(f):
    while True:
        raw = array.array('c')
        raw.fromstring(f.read(23))
        if not raw:
            break
        yield raw

Use 'c' to interpret the values as a character. 'B' as an unsigned char i.e. 0-255.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124538

Read the file in 23 byte chunks:

from functools import partial

with open('sample.txt', 'rb') as inputfile, open(outputfilename, 'wb') as output:
    for chunk in iter(partial(inputfile.read, 23), ''):
        # chunk is 23 bytes small
        output.write(chunk + '\n')

Here we use the iter() function with a sentinel to loop over a function until that function returns '', the empty string. The function we loop over is using functools.partial() to call inputfile.read() with the argument 23 each and every time. You could use a lambda (lambda: inputfile.read(23)) instead, but partial() is a faster.

Upvotes: 3

Related Questions