TransWikia.com

How would I create a new line in a text document after the nth word?

Stack Overflow Asked by prxvidxnce on December 21, 2020

I am working on a project in which I have to list a whole lot of things out in this format

[*] This is the first object
[*] This is the second object
[*] When the object gets too long, I need to move it to the next line
    to look like this. 

I created a .py file and a .txt file. I would input the list objects into the .py file and it would properly format it into the text file. I tried something like this:

text = input('> ')
with open('list.txt', 'a') as f:
    f.write(f'[*] {t}n')

But I can’t seem to move it to the next line. Around the 15th word, is when I want to go to a new line.

How can I achieve this?

3 Answers

Print the bullet first. Then split the text into a list of words, and print each one in a loop. Inside the loop, keep track of how many words have been printed on the current line, and of you reach 15, print a newline and reset the count to zero.

with open('list.txt', 'a') as f:
    # print the bullet
    f.write('[*]')

    wordcount = 0

    # loop over each word in the text
    for word in text.split():
        f.write(f' {word}')
        wordcount += 1

        # if this line has 15 words, start a new line
        if wordcount == 15:
            f.write('n   ')
            wordcount = 0

    # finally write a newline
    f.write('n')

Correct answer by John Gordon on December 21, 2020

Or use list comprehensions as an alternative. It is fast, short and does not require importing libraries.

text = 'When the object gets too long, I need to move it to the next line to look like this or even to the next next line if the object gets really long.'
with open('list.txt', 'a') as f:
    words = text.split()
    words = [w if (n + 1) % 15 else w + 'n   ' for n, w in enumerate(words)]
    text = ' '.join(words)
    f.write(f'[*] {text}n')

Will result in:

[*] When the object gets too long, I need to move it to the next line
    to look like this or even to the next next line if the object gets
    really long.

Answered by hoke1606 on December 21, 2020

Sounds like you're looking for the built-in textwrap module:

import textwrap

wrapper = textwrap.TextWrapper()
wrapper.width = 70
wrapper.initial_indent    = '[*] '
wrapper.subsequent_indent = '    '

items = [
    'This is the first object',
    'This is the second object',
    'When the object gets too long, I need to move it to the next line to look like this.',
]

with open('list.txt', 'a') as f:
    for item in items:
        f.write(wrapper.fill(item) + 'n')

Which produces:

[*] This is the first object
[*] This is the second object
[*] When the object gets too long, I need to move it to the next line
    to look like this.

Answered by Brian Rodriguez on December 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP