Python separator lines

I have a large text file for a line, I would like to split the line every 117 characters and put the next 117 characters in a new line and so on until the end of the file.

I tried this: `s =" "" I deleted the line for reasons of visibility "" "space =" ""

"" file = open ('testoutput.txt', 'w') while s: print s [: 10] output = output + s + ""

""" s = s[10:] 

file.write (exit) file.close () print "Finish" `

but had a problem when the end result in the file looked like this cascading: "this [SHIFT] r [BACKSPACE] molecule and its descendants will be av [BACKSPACE] [BACKSPACE] change due to mutations

 T]r[BACKSPACE]molecule and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations ACE]molecule and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations le and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations descendants would av[BACKSPACE][BACKSPACE]vary because of mutations ts would av[BACKSPACE][BACKSPACE]vary because of mutations v[BACKSPACE][BACKSPACE]vary because of mutations E][BACKSPACE]vary because of mutations CE]vary because of mutations cause of mutations utations 

`

+4
source share
2 answers
 while s: print s[:117] s = s[117:] 
+6
source

You can either split the buffer using the usual cut syntax, or you can choose to split the file directly when reading. This is an example of a second approach:

 with open(r"/path/to/some/file") as input_file: line = input_file.read(117) while line: print len(line) line = input_file.read(117) 
+3
source

Source: https://habr.com/ru/post/1338202/


All Articles