Little Help With This Python Function - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Little Help With This Python Function (1404 Views)
| 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: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: somehow this gives me an error, im using python 2.7but this worked w = w.replace('\n','<br />\n') thanks. ekt_bear: Looks like you are already handling the '\n' case in your code?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 |
Python tutorial on regex: http://code.google.com/edu/languages/google-python-class/regular-expressions.html |
Help With This Python Algorithm.... • I Need HELP With This Python Problem • Please Help With This Python Code • 2 • 3 • 4
Help On SQL!!! • How Can I Change A Label Display • Object Constraint Language (OCL)