2

Ahoy StackOverlow-ers!

I have a rather trivial question but it's something that I haven't been able to find in other questions here or on online tutorials: How might we be able to format the output of a Python program that so that it fits a certain aesthetic format without any extra modules?

The aim here is that I have a block of plain text like that from a newspaper article, and I've filtered through it earlier to extract just the words I want but now I'd like to print it out in the format that each line only has 70 characters along it and any word won't be broken if it should normally fall on a line break.

Using .ljust(70) as in stdout.write(article.ljust(70)) doesn't seem to do anything to it.

The other thing about not having words broken would be as:

Latest news tragic m

urder innocent victi

ms family quiet neig

hbourhood

Looking more like this:

Latest news tragic

murder innocent

victims family 

quiet neighbourhood

Thank you all kindly in advance!

eumiro
  • 207,213
  • 34
  • 299
  • 261
user1359892
  • 31
  • 1
  • 4
  • I think this is duplicate of this: http://stackoverflow.com/questions/250357/smart-truncate-in-python – jgritty May 08 '12 at 08:26
  • 1
    Also, for newspaper's and typesetting, see this: http://en.wikipedia.org/wiki/Kerning – jgritty May 08 '12 at 08:28
  • Never mind, textwrap seems like a good fit. – jgritty May 08 '12 at 08:39
  • @mooeeeep No, I'm just looking to learn something new do I've picked up a copy Python for dummies and I'm going through it in my spare time. Just looking to fill some gaps in what I've read is all. – user1359892 May 08 '12 at 23:41

3 Answers3

8

Checkout the python textwrap module (a standard module)

>>> import textwrap
>>> t="""Latest news tragic murder innocent victims family quiet neighbourhood"""
>>> print "\n".join(textwrap.wrap(t, width=20))
Latest news tragic
murder innocent
victims family quiet
neighbourhood
>>> 
Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132
  • Thanks for the answer! It worked a treat but I'm wondering if it's also possible to do something similar without the textwrap module? I expect it may take a lot more effort though wouldn't it? – user1359892 May 08 '12 at 08:52
  • Why would you not use textwrap? – Marcin May 08 '12 at 09:19
  • If you want to write your own code for this specific case I think that's fine too. It shouldn't be difficult at all. It will use only standard python functions. – Abhranil Das May 08 '12 at 09:29
  • @user1359892: Have a look at the Lib/textwrap.py. It is written in Python. You can study how it works. – pepr May 08 '12 at 11:20
  • Thanks pepr and Abhranil I'll have to look into both and see if I can figure out how the module works =) – user1359892 May 08 '12 at 23:42
0

use textwrap module:

http://docs.python.org/library/textwrap.html

HYRY
  • 94,853
  • 25
  • 187
  • 187
0

I'm sure this can be improved on. Without any libraries:

def wrap_text(text, wrap_column=80):
    sentence = ''
    for word in text.split(' '):
        if len(sentence + word) <= 70:
            sentence += ' ' + word
        else:
            print sentence
            sentence = word
    print sentence

EDIT: From the comment if you want to use Regular expressions to just pick out words use this:

import re

def wrap_text(text, wrap_column=80):
    sentence = ''
    for word in re.findall(r'\w+', text):
        if len(sentence + word) <= 70:
            sentence += ' ' + word
        else:
            print sentence
            sentence = word
    print sentence
satran
  • 1,222
  • 4
  • 16
  • 27
  • Thanks, Satyajit! I'll have to give this a go. It looks just like what I was imagining. And instead of the word.split() do you think it'd be ok to use a regex to maybe refine where and how I split it? – user1359892 May 08 '12 at 23:43
  • Yes definitely. If you want just the words and not the punctuations (this answer)[http://stackoverflow.com/a/1059596/504262] would help. Use ``re.findall(r'\w+',text)``. – satran May 09 '12 at 04:51