473,785 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting a string to be a columned block of text

Hi,

I'm creating a python script that can take a string and print it to the
screen as a simple multi-columned block of mono-spaced, unhyphenated
text based on a specified character width and line hight for a column.
For example, if i fed the script an an essay, it would format the text
like a newspaper on the screen. Text processing is very new to me, any
ideas on how I could achieve a multi-columned text formatter. Are there
any libraries that already do this?

Thanks and happy holidays!
Leon

Dec 26 '06 #1
7 2694

Leon wrote:
Hi,

I'm creating a python script that can take a string and print it to the
screen as a simple multi-columned block of mono-spaced, unhyphenated
text based on a specified character width and line hight for a column.
For example, if i fed the script an an essay, it would format the text
like a newspaper on the screen. Text processing is very new to me, any
ideas on how I could achieve a multi-columned text formatter. Are there
any libraries that already do this?

Thanks and happy holidays!
Leon
I did something similar, read in text file format it to 79 characters
and print it out to a multiple images of 480x272 so i could read them
on my PSP. Anyway the code to handle the formatting (even though this
is one column of 78 characters with one character space from the top
and bottom, and sides)

corpus = open("essay.txt ","r")

wordcount = 0
pctline = ""
pctlines = [

for line in corpus.readline s():
# i dont need the newline character as it will be printed onto
images
line = line[:-1]

# i want individual words where each word is separated by a space
character
words = line.split(" ")

for word in words:
if wordcount + len(word) + 1 < 78:
wordcount = wordcount + len(word) + 1
pctline = pctline + word + " "
else:
wordcount = len(word)

pctlines.append (pctline)
pctline = None
pctline = word + " "

corpus.close()

what it does is keeps a list of words and iterate through it and see if
the length of that word and one space character and the line doesn't
exceed 78, if it doesnt then add it to the pctlines, add the count to
the wordcount + 1. If it does then we have one line either 78
characters long or less and add it to pctlines which is a list of all
the lines.

Hope this helps.
Cheers

Dec 26 '06 #2
"Leon" <pe******@gmail .comwrote in message
news:11******** **************@ h40g2000cwb.goo glegroups.com.. .
Hi,

I'm creating a python script that can take a string and print it to the
screen as a simple multi-columned block of mono-spaced, unhyphenated
text based on a specified character width and line hight for a column.
For example, if i fed the script an an essay, it would format the text
like a newspaper on the screen. Text processing is very new to me, any
ideas on how I could achieve a multi-columned text formatter. Are there
any libraries that already do this?

Thanks and happy holidays!
Leon
Check out the textwrap module, new in 2.3. Here is code to do one- and
two-column output.

-- Paul
gettysburgAddre ss = """Four score and seven years ago our fathers brought
forth on this continent, a new nation, conceived in Liberty, and dedicated
to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any
nation so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field,
as a final resting place for those who here gave their lives that that
nation might live. It is altogether fitting and proper that we should do
this.
But, in a larger sense, we can not dedicate -- we can not consecrate -- we
can not hallow -- this ground. The brave men, living and dead, who struggled
here, have consecrated it, far above our poor power to add or detract. The
world will little note, nor long remember what we say here, but it can never
forget what they did here. It is for us the living, rather, to be dedicated
here to the unfinished work which they who fought here have thus far so
nobly advanced. It is rather for us to be here dedicated to the great task
remaining before us -- that from these honored dead we take increased
devotion to that cause for which they gave the last full measure of
devotion -- that we here highly resolve that these dead shall not have died
in vain -- that this nation, under God, shall have a new birth of freedom --
and that government of the people, by the people, for the people, shall not
perish from the earth.
""".split(' \n')

import textwrap

# wrap text at 50 characters
for line in gettysburgAddre ss:
print "\n".join( textwrap.wrap(l ine,50) )
print

# create two columns of text
wrappedLines = sum([ textwrap.wrap(l ine,35) + ['']
for line in gettysburgAddre ss ],[])[:-1]
numLines = len(wrappedLine s)
halfway = numLines/2
twoCol = [ "%-38s%s" % pair for pair in
zip(wrappedLine s[:halfway],wrappedLines[halfway:]) ]
print "\n".join(twoCo l)

Dec 26 '06 #3
On 26 Dec 2006 04:14:27 -0800, Leon <pe******@gmail .comwrote:
I'm creating a python script that can take a string and print it to the
screen as a simple multi-columned block of mono-spaced, unhyphenated
text based on a specified character width and line hight for a column.
Hi, Leon,
For putting the columns together zip is your friend. Let me lay out an example:

# get your text and strip the newlines:
testdata = file('something ').read().repla ce('\n','')
# set some parameters (these are arbitrary, pick what you need)::
colwidth = 35
colheight = 20
numcol = 2
rowperpage = colheight * numcol
# first split into lines (this ignores word boundaries
# you might want to use somehting more like placid posted for this)
data1 = [testdata[x:x+colwidth] for x in range(0,len(tes tdata),colwidth )]
# next pad out the list to be an even number of rows - this will give
# a short final column. If you want them balanced you're on your own ;)
data1.extend(['' for x in range(rowsperpa ge - len(data1) % rowsperpage)])
# then split up the list based on the column length you want:
data2 = [data1[x:x+colheight] for x in range(0,len(dat a1),colheight)]
# then use zip to transpose the lists into columns
pages = [zip(*data2[x:x+numcol]) for x in range(0,len(dat a2),numcol)]
# finally unpack this data with some loops and print:
for page in pages:
for line in page:
for column in line:
print ' %s ' % column, #<- note the comma keeps newlines out
print '\n'
print '\f'
-dave
Dec 26 '06 #4
Thanks, Paul. I didn't know about textwrap, that's neat.

Leon,
so in my example change
data1= [testdata[x:x+colwidth] for x in range(0,len(tes tdata),colwidth )]
to
data1 = textwrap.wrap(t estdata,colwidt h)
data1 = [x.ljust(colwidt h) for x in data1]
oh and I made a mistake that double spaces it. the "print '\n'"
line needs to be either
print ''
or
print '\n',
(with a comma)

-dave
Dec 26 '06 #5
"Paul McGuire" <pt***@austin.r r._bogus_.comwr ote in message
news:45******** *************** @roadrunner.com ...
>
gettysburgAddre ss = """Four score and seven years ago...
<snip>

By the way, this variable contains only 3 (very long) lines of text, one for
each paragraph. (Not immediately obvious after Usenet wraps the text.)

-- Paul
Dec 26 '06 #6
"Paul McGuire" <pt***@austin.r r._bogus_.comwr ote:
By the way, this variable contains only 3 (very long) lines of text,
one for each paragraph. (Not immediately obvious after Usenet wraps
the text.)
Usenet doesn't wrap text, all it has is a convention which suggests that
people posting to usenet should wrap their text at 72 characters. Your
newsreader probably wrapped the text for you when you were posting, but
for deliberately long lines many newsreaders will have an option
somewhere to turn off the text wrapping (and the reader probably also
has to turn off text wrapping when they read it).

For source code where you want long unwrapped strings you would have
been better to do the text wrapping in an editor first and use backslash
line continuations.

e.g.
gettysburgAddre ss = """\
Four score and seven years ago our fathers brought forth on this \
continent, a new nation, conceived in Liberty, and dedicated to the \
proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, \
or any nation so conceived and so dedicated, can long endure. We are \
met on a great battle-field of that war. We have come to dedicate a \
portion of that field, as a final resting place for those who here \
gave their lives that that nation might live. It is altogether fitting \
and proper that we should do this.
But, in a larger sense, we can not dedicate -- we can not consecrate \
-- we can not hallow -- this ground. The brave men, living and dead, \
who struggled here, have consecrated it, far above our poor power to \
add or detract. The world will little note, nor long remember what we \
say here, but it can never forget what they did here. It is for us the \
living, rather, to be dedicated here to the unfinished work which they \
who fought here have thus far so nobly advanced. It is rather for us \
to be here dedicated to the great task remaining before us -- that \
from these honored dead we take increased devotion to that cause for \
which they gave the last full measure of devotion -- that we here \
highly resolve that these dead shall not have died in vain -- that \
this nation, under God, shall have a new birth of freedom -- and that \
government of the people, by the people, for the people, shall not \
perish from the earth.\
""".split(' \n')
Dec 26 '06 #7
"Dave Borne" <db****@gmail.c omwrote in
news:ma******** *************** *************** *@python.org:
Thanks, Paul. I didn't know about textwrap, that's neat.

Leon,
so in my example change
>data1= [testdata[x:x+colwidth] for x in
range(0,len(te stdata),colwidt h)]
to
>data1 = textwrap.wrap(t estdata,colwidt h)
data1 = [x.ljust(colwidt h) for x in data1]

oh and I made a mistake that double spaces it. the "print '\n'"
line needs to be either
print ''
or
print '\n',
(with a comma)
The solutions so far serve up text that is split within the
confines of the column width, but leave a "ragged right" margin,
where the line lengths appear to differ. I had the impression that
the OP wanted right-and-left justified text, such as would
typically be found in a newspaper column. Here's a shot at doing
that for fixed-width fonts. To use it, first split your lines as
others have suggested, then print aline(line,colw id) for each line
in your data.

# aline - align text to fit in specified width
# This module arbitrarily chooses to operate only on long-enough
# lines, where "long-enough" is defined as 60% of the specified
# width in this case.
#
def aline(line, wid):
line = line.strip()
lnlen = len(line)
if wid lnlen wid*0.6:
ls = line.split()
diff = wid - lnlen
#nspaces = len(ls)-1
eix = 1
bix = 1
while diff 0: # and nspaces 0:
if len(ls[bix]) == 0 or ls[bix].find(' ') >= 0:
ls[bix] += ' '
else:
ls.insert(bix,' ')
diff -= 1
bix += 2
if bix >= 1+len(ls)/2: bix = 1

if diff 0:
if len(ls[-eix]) == 0 or ls[-eix].find(' ') >= 0:
ls[-eix] += ' '
else:
ls.insert(-eix,'')
diff -= 1
eix += 2
if eix >= 1+len(ls)/2: eix = 1
line = ' '.join(ls)
return line

--
rzed
Dec 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2841
by: Lorentz | last post by:
Greetings. I have a process that takes data from a MS Access2000 database and produces .pdf files. Everything works fine... but I'm trying to format the header and footer of the pdf file so that the a line can contain 2 tex-alignments (center and right). Thus far, I've had no success in achieving this criteria. Does anyone have any suggestions...? Here's what I have so far (the result is on 2 separate lines):
7
398
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some other country. When I retrieve the phone numbers, I need to display them as (###) ###-#### x 99999 if it is a Canadian number or (###) ###-#### Ext. 99999 if it is US phone number. Potentially I'd have other countries added as well which might have...
7
4674
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is quite ugly. The black area is larger then it is once an item is added. How can I solve this problem either by: A) Having only the header black and the rest of the empty block white or gray (preffered solution) B) Having the color gray or white...
4
3236
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
8
9181
by: G.Ashok | last post by:
Hi, I have created CultureInfo object and specified required digit grouping in it. The one of the overloaded ToString methods of Decimal type has parameters to format the value with required custom format and a IFormatProvider. I pass a custom format string for positive, negative and Zero (3 sections) and CultureInfo object containing the required DigitGrouping as IFormatProvider. But ToString is not formatting the value using the digit...
25
2711
by: mdh | last post by:
Hi Group, Not looking for an answer, but more of an explanation. Thinking back to those heady days when you had the time to do them, may I ask this. Exercise 1-22 asks for a program to "fold" long input lines into 2 or more shorter lines before the nth column... etc etc. Now, there are numerous anwers on the web and in the "C answer book", which includes a function to expand the tabs to blanks ( depending upon where the tab is in...
9
3209
by: sck10 | last post by:
Hello, I have a page with an ImageButton that is used to redirect to another page. When the page first opens, everything looks as expected. However, when I click on the image, the new page opens as expected. However, when I go back to the original page, all the font sizes are larger. Its as if by clicking on the ImageButton, my CSS formatting was discarded. When I look at the source for the following code, this is what I get:
14
4361
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my unformatted data values (as decimals) just fine, so I know there's not a problem with the data retrieval, just the formatting. I have read that this would work: lblPrice.Text = prodRow.ToString("C");
7
2094
by: Andy_Khosravi | last post by:
I use the formatting trick in A97 to simulate conditional formatting in continuous forms. It has, up to this point, worked fine. Today I was making some design changes to the parent form (the continuous form is a sub form within this parent form) when the formatting trick I was using stopped working. What's happening is that the character I use to 'block over' fields and make them appear to be invisible stopped displaying a solid bar...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.