₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,151 members, 8,420,571 topics. Date: Friday, 05 June 2026 at 02:38 AM

Toggle theme

Little Help With This Python Function - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingLittle Help With This Python Function (1404 Views)

1 Reply (Go Down)

Little Help With This Python Function by PrinceNN(op): 11:08am On May 30, 2012
hello all, im new to python web programming and i have a little difficulty with this:

i just implemented the rot13 cipher here, but the problem is
How do i preserve the formatting of the text inserted using <textarea> of the form?
Normally all the spaces and lines are removed in the received message to give one continuous line of result.

Php has the nl2br function, but i am not using php in this case, python

the whole source code of is in one file here.
Re: Little Help With This Python Function by Seun(mod): 11:18am On May 30, 2012
text = text.replace('\n').('<br>')
Re: Little Help With This Python Function by ektbear: 11:44am On May 30, 2012
Looks like you are already handling the '\n' case in your code?

if is_xhtml:
return string.replace('\n','<br />\n')
else :
return string.replace('\n','<br>\n')
Or do you want something that behaves exactly like the PHP function (also handling '\r\n', etc)?
Re: Little Help With This Python Function by ektbear: 12:13pm On May 30, 2012
Here is a Ruby implementation of this nl2br() function that takes care of those additional cases.

http://pastebin.com/R7XFpn6Z

Shouldn't be hard for you to port to Python. Key detail is this negative lookbehind/lookahead regular expression.

I tried it out on the examples in the PHP nl2br() documentation, seems to pass those.

However, it will fail if it has something like "\n\r\n."

I'm actually not sure how you want the behavior to be defined in that case.
Re: Little Help With This Python Function by PrinceNN(op): 1:13pm On May 30, 2012
Seun:
text = text.replace('\n').('<br>')
somehow this gives me an error, im using python 2.7

but this worked
w = w.replace('\n','<br />\n')
thanks.

ekt_bear: Looks like you are already handling the '\n' case in your code?
Or do you want something that behaves exactly like the PHP function (also handling '\r\n', etc)?
yea thanks, your ruby implementation was helpful. tho im only going to use that single line for this app. thnx again
Re: Little Help With This Python Function by ektbear: 4:41am On May 31, 2012
1 Reply

Help With This Python Algorithm....I Need HELP With This Python ProblemPlease Help With This Python Code234

Help On SQL!!!How Can I Change A Label DisplayObject Constraint Language (OCL)