PDA

View Full Version : Using wordwrap or reading in newline (\n) comands from a .txt document


EnvisMJ
04-04-2011, 03:21 PM
I'm reading in text from a .txt document that is to be read in as a string, and displayed within an infobox on screen. Problem is, some of the text chunks that need to be displayed are short paragraphs, so when the infobox shows up, it spans the screen, and only some of the text is visible.

How do I format this text into manageable lines?

A. I've added '\n' within the .txt file to format it, but it's just ignoring them and actually writing out the \n when displayed. Is there a way to get it to not ignore them?

B. Is there a wordwrap option that I can use? or even just a way of adding a newline every X number of characters?

farshizzo
04-04-2011, 03:52 PM
If you are manually placing the \n characters in your text file, then you will need to use the following code to manually escape special characters (e.g \n):s = s.decode('string_escape')

Alternatively, you could use the textwrap module to automatically wrap the text:import textwrap
s = '\n'.join(textwrap.wrap(s,100)) #Wrap every 100 characters