I've got a string that gets imported from a text file, with the following
code: input_file.open("text_file.txt", 'r')
text = input_file.read()
input_file.close()
Now my problem is that the string is going to be between 1-999 long. I need to somehow have a space after every 10th letter, and a new line every 60th letter.
At the moment I've tried using:
def insertNewLines(text, lineLength):
if len(text) <= lineLength:
return text
else:
return text[:60] + ' '.join(text[i:i+10] for i in range(0,len(text),10)) + insertNewLines(text[60:],60)
What I want to do is import a sequence of letters (1-999) from a text file, then use python to import the sequence into an HTML file, which then writes an HTML file with the imported sequence colorcoded/numbered/splitup and in a viewable format. I've already got the HTML file working, but I can't get the text to have a space after 10 letters and a new line after every 60th.
This is what my current attempt looks like .
You can accomplish this by using join
and stepping through every nth
character using range
. Once your text is split up the way you want it, you can then split it on the \\n
and then insert the p
elements in between each line.
f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' * 35 #this generates a 910 character string
def break_apart(sep, step):
return sep.join(f[n:n + step] for n in range(0, len(f), step))
f = break_apart(' ', 10)
f = break_apart('\n', 60)
p = '<p>1 10 20 30 40 50 60</p>'
fp = '\n'.join(j for i in f.split('\n') for j in [p, i])
html = '<div>{}</div>'.format(fp)
print(html)
<div>
<p>1 10 20 30 40 50 60<p>
ABCDEFGHIJ KLMNOPQRST UVWXYZABCD EFGHIJKLMN OPQRSTUVWX YZABC
<p>1 10 20 30 40 50 60<p>
DEFGH IJKLMNOPQR STUVWXYZAB CDEFGHIJKL MNOPQRSTUV WXYZABCDEF
<p>1 10 20 30 40 50 60<p>
GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ KLMNOPQRST UVWXYZABCD EFGH
<p>1 10 20 30 40 50 60<p>
IJKLMN OPQRSTUVWX YZABCDEFGH IJKLMNOPQR STUVWXYZAB CDEFGHIJK
<p>1 10 20 30 40 50 60<p>
L MNOPQRSTUV WXYZABCDEF GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ KLM
<p>1 10 20 30 40 50 60<p>
NOPQRST UVWXYZABCD EFGHIJKLMN OPQRSTUVWX YZABCDEFGH IJKLMNOP
<p>1 10 20 30 40 50 60<p>
QR STUVWXYZAB CDEFGHIJKL MNOPQRSTUV WXYZABCDEF GHIJKLMNOP QR
<p>1 10 20 30 40 50 60<p>
STUVWXYZ ABCDEFGHIJ KLMNOPQRST UVWXYZABCD EFGHIJKLMN OPQRSTU
<p>1 10 20 30 40 50 60<p>
VWX YZABCDEFGH IJKLMNOPQR STUVWXYZAB CDEFGHIJKL MNOPQRSTUV W
<p>1 10 20 30 40 50 60<p>
XYZABCDEF GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ KLMNOPQRST UVWXYZ
<p>1 10 20 30 40 50 60<p>
ABCD EFGHIJKLMN OPQRSTUVWX YZABCDEFGH IJKLMNOPQR STUVWXYZAB
<p>1 10 20 30 40 50 60<p>
CDEFGHIJKL MNOPQRSTUV WXYZABCDEF GHIJKLMNOP QRSTUVWXYZ ABCDE
<p>1 10 20 30 40 50 60<p>
FGHIJ KLMNOPQRST UVWXYZABCD EFGHIJKLMN OPQRSTUVWX YZABCDEFGH
<p>1 10 20 30 40 50 60<p>
IJKLMNOPQR STUVWXYZAB CDEFGHIJKL MNOPQRSTUV WXYZABCDEF GHIJ
<p>1 10 20 30 40 50 60<p>
KLMNOP QRSTUVWXYZ ABCDEFGHIJ KLMNOPQRST UVWXYZABCD EFGHIJKLM
<p>1 10 20 30 40 50 60<p>
N OPQRSTUVWX YZABCDEFGH IJKLMNOPQR STUVWXYZAB CDEFGHIJKL MNO
<p>1 10 20 30 40 50 60<p>
PQRSTUV WXYZABCDEF GHIJKLMNOP QRSTUVWXYZ
</div>
I assume your 'text_file' has Newlines in it, read line by line.
text = []
# My Sample Text files are utf-8 encoded
with io.open( 'text_file.txt','r', encoding='utf8') as fh:
for row,line in enumerate(fh,1):
text.append(line)
#end for
#end with = closing fh
Tested with Python:3.4.2
Assuming a consistent string of text as input:
import string
import random
text = ''
for i in range(16):
text += random.choice(string.ascii_lowercase)*10
def insert_new_lines(input_string):
output = []
for i in range(0, len(text), 60):
output.append(' '.join([text[j:j+10] for j in range(i, i+60, 10)]))
return '\n'.join(output)
print insert_new_lines(text)
returns, using random letters as an example:
ssssssssss mmmmmmmmmm mmmmmmmmmm wwwwwwwwww zzzzzzzzzz aaaaaaaaaa
llllllllll llllllllll ffffffffff rrrrrrrrrr cccccccccc nnnnnnnnnn
rrrrrrrrrr xxxxxxxxxx uuuuuuuuuu qqqqqqqqqq
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.