Newbie, homework help, please.

duncan smith buzzard at urubu.freeserve.co.uk
Sat Apr 21 20:29:36 EDT 2012


On 21/04/12 23:48, BartC wrote:
> "someone" <wesbroom at gmail.com> wrote in message
> news:9533449.630.1335042672358.JavaMail.geo-discussion-forums at ynmf4...
>> On Saturday, April 21, 2012 3:44:49 PM UTC-5, BartC wrote:
>
>> Hi, Bart: Thank you, your post is working now, maybe, I did something
>> wrong, unfortunately, you are right, my setup for getting the file to
>> pull
>> up correctly now is an issue, At first, I got a Vertical line with it
>> working, then I tried to tinker with it, and it fratched, lol
>> def border(text):
>> maxwidth=0
>> for s in text:
>> if len(s)>maxwidth: maxwidth=len(s)
>> vertinchlines=6 # assume 6 lines/inch
>> hozinchchars=10 # assume 10 chars/inch
>> hozmargin=" "*hozinchchars
>> newtext=[]
>> for i in range(vertinchlines):
>> newtext.append("")
>> newtext.append(hozmargin+"*"*(maxwidth+4)+hozmargin)
>> newtext.append(hozmargin+"* "+" "*maxwidth+" *"+hozmargin)
>> for s in text:
>> newtext.append(hozmargin+"* "+s+" "*(maxwidth-len(s))+" *"+hozmargin)
>> newtext.append(hozmargin+"* "+" "*maxwidth+" *"+hozmargin)
>> newtext.append(hozmargin+"*"*(maxwidth+4)+hozmargin)
>> for i in range(vertinchlines):
>> newtext.append("")
>> return newtext
>> x=textfile;indat=open(x,'r');SHI=indat.read()
>> textTuple = border(SHI)
>> for lines in textTuple:
>> print ("%s\n" %textTuple)
>>
>>
>> The issue is trying to get the SHI to work right, but omg, this is the
>> closes I have gotten, you are awsome, thank you very much, i guess i will
>> just keep messing with it till i get it
>
> I had to use this code to make this work right from a file (in additon
> to the border() function):
>
> textfile="kkk4" # (don't use this; this was my test input)
>
> x=textfile;indat=open(x,'r');
>
> SHI=indat.readlines()
> indat.close()
>
> for i in range(len(SHI)): # remove trailing '\n' from each line
> s=SHI[i]
> SHI[i]=(s[0:len(s)-1])
>
> textTuple = border(SHI)
>
> for lines in textTuple:
> print (lines)
>
> Your indat.read() seemed to read all the lines as one long string. I used
> indat.readlines() instead. However each line has a newline char '\n' at the
> end. I put in a loop to get rid of that (I'm sure there's a one-line fix to
> do that, but as I said don't know Python).
>

[snip]

line = line.rstrip()

would do the job. And as file objects are iterable,

indat = open(x,'r')
SHI = [line.rstrip() for line in indat]
indat.close()

textTuple = border(SHI)

etc.

Duncan



More information about the Python-list mailing list