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 Answer 1

0

use,

all_lines = all_lines + htmlLine + "\n"
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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 = [].
No it's supposed to be a string. One big string with those newlines inserted. Is that possible?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.