简体   繁体   English

第n个字符后的空格和换行符

[英]Space and new line after every n'th character

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. 现在我的问题是字符串长度将在1-999之间。 I need to somehow have a space after every 10th letter, and a new line every 60th letter. 我需要以某种方式在第10个字母后留一个空格,并在第60个字母后换行。

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. 我想做的是从文本文件中导入字母序列(1-999),然后使用python将序列导入到HTML文件中,然后再使用导入的序列以彩色编码/编号/分割的方式写入HTML文件,并在可见的格式。 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. 我已经可以使用HTML文件了,但是我无法使文本在10个字母之后有一个空格,而在每60个字母之后没有一个新行。

This is what my current attempt looks like . 这就是我目前的尝试看起来

You can accomplish this by using join and stepping through every nth character using range . 您可以通过使用join并使用range逐步遍历nth字符来完成此操作。 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. 将文本按所需方式拆分后,可以在\\n上拆分它,然后在每行之间插入p元素。

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_file”中包含换行符,请逐行阅读。

  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 使用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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM