返回插入了换行符的String

时间:2012-06-01 21:03:39

标签: python-3.x

我有一个函数可以返回另一个文件选中并使用的HTML代码。但是,代码返回一个大段,我需要将它分成适当的行。这对打印来说很容易,但是我需要以这种方式返回它,以便下一个文件可以拾取它。

立即输出:

'<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;'

期望的输出:

<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;

等.....

这是我正在使用的功能:

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 个答案:

答案 0 :(得分:0)

使用,

all_lines = all_lines + htmlLine + "\n"