0

I have a function that returns HTML code that another file picks up and uses. However, the code is returned in one big paragraph, and I need it to be separated into appropriate lines. This is easy enough with print, but I need to return it that way in order for the next file to pick it up.

Output right now:

'<abbr title = 6 style="font-size: 800%">spam</abbr> &nbsp; &nbsp;<abbr title = 5 style="font-size: 800%">page</abbr> &nbsp; &nbsp;<abbr title = 4 style="font-size: 800%">penguin</abbr> &nbsp; &nbsp;<abbr title = 2 style="font-size: 800%">stem</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">has</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">love</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">these</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">spamming</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">spammer</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">your</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">webpage</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">program</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">stemming</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">stemmer</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">were</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 800%">word</abbr> &nbsp; &nbsp;<abbr title = 1 style="font-size: 1600.0%">by</abbr> &nbsp; &nbsp;'

Desired output:

<abbr title = 6 style="font-size: 800%">spam</abbr> &nbsp; &nbsp;

<abbr title = 5 style="font-size: 800%">page</abbr> &nbsp; &nbsp;

<abbr title = 4 style="font-size: 800%">penguin</abbr> &nbsp; &nbsp;

etc.....

Here's the function I'm using:

def mtcURL(URL):
a = getURL(URL)
COUNT = a[0]
WORD = a[1]
NUMBER = 800
i = 0
all_lines = ""

while i < len(a[1]):
    if i == len(COUNT)-1:
        ratio = COUNT[i] / 2
        NUMBER = NUMBER / (ratio)
    else:
        try:
            ratio = COUNT[i]/COUNT[i+1]
        except IndexError:
            pass
    first = '<abbr title = '
    second = 'style="font-size: '
    third = '%">'
    fourth = '</abbr> &nbsp; &nbsp;'
    htmlLine = first + str(COUNT[i])+ ' ' + second + str(NUMBER)+ third + WORD[i] + fourth    
    all_lines += htmlLine
    i += 1
return all_lines

1 Answers1

0

use,

all_lines = all_lines + htmlLine + "\n"
Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
uselpa
  • 18,732
  • 2
  • 34
  • 52
  • Thanks for the reply. I just tried that, and all it did was add the string "\n" to the end of each line. It's still in one big paragraph though. – Adrian Andronic Jun 01 '12 at 21:20
  • So you want to return a list? Then finish by "return all_lines.split("\n"). Or leave out the \n stuff and initialize all_lines = []. – uselpa Jun 01 '12 at 21:25
  • No it's supposed to be a string. One big string with those newlines inserted. Is that possible? – Adrian Andronic Jun 01 '12 at 21:28
  • Then my first solution works - \n is the newline character. Depending on how you look at it, it will show as a long string with embedded \n characters, or as several lines. Print("aaa\nbbb") yields 2 lines, print(repr("aaa\nbbb")) only one. – uselpa Jun 01 '12 at 21:32
  • No, in Python \n is universal if the file is opened in text mode. If you are on Windows, it will automatically be replaced by \r\n. On Unix it will be left as is. – uselpa Jun 19 '12 at 19:04