1

I need to read a .txt file with python 3.3 line by line and split the length of characters (format: MEHSAOSHAHSHSUDO....) into 513 character chunks. My code works for the first line however the nextline does not work and I cannot work out why.

I am also confused on how to get the code to repeat the nextline function until the end of the file. The file is around 500 lines long.

This is what I have so far:

with open('bsxlength.txt' , 'r') as string:
    first_line = string.readline()
    n = 513
    print [first_line[i:i+n] for i in range(0, len(first_line), n)]
    next_line = string.readline(+2)
    n = 513
    print [next_line[i:i+n] for i in range(0, len(next_line), n)]

Thankyou

The ultimate goal is to make it split the lines into 513 chunks and if there is not enough for a chunk e.g. the line is 600 letters long to count back the appropriote amount of letters (e.g. 87) and make a new chunk. But one step at a time eh

M.Smith12
  • 51
  • 6

1 Answers1

4
next_line = string.readline(+2)

means "read a maximum of 2 characters from the current line", not "read the second line". You also don't have to manually repeat the same commands for each line - just use a loop:

with open('bsxlength.txt' , 'r') as string:
    n = 513
    for line in string:
        print [line[i:i+n] for i in range(0, len(line), n)]

Now back to your initial approach: The optional parameter size that .readline() takes specifies the number of characters that should at most be read from the current line. So you can use that to achieve your goal:

with open('bsxlength.txt' , 'r') as string:
    result = []
    while True:
        chunk = string.readline(513)
        if chunk:
            result.append(chunk)
        else:
            break
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thankyou so much! Any clue on how to countback at all (see edit one) – M.Smith12 Feb 26 '16 at 14:50
  • Sorry, I mistyped; it should be `for line in string`, not `file`. See my edit. – Tim Pietzcker Feb 26 '16 at 14:51
  • Probably a silly question but how do I get that to print (I am using Canopy Enthought (for lack of any other software)) and it seems to only display the output when the term print is used. – M.Smith12 Feb 26 '16 at 14:58
  • and will this leave any line that are less than 513 characters long untouched.As I only need to split those that are more. Any smaller fragments that occur naturally are still needed. – M.Smith12 Feb 26 '16 at 15:04
  • You could always just `print(chunk)` during the loop or `print(result)` afterwards. And no, it won't discard shorter lines - as I explained above, `size` specifies the *upper* limit of characters to be read from the current line. – Tim Pietzcker Feb 26 '16 at 15:19