Вопрос

I am trying to write a program that highlights hashtags in tweets. but the program will fail if the tweet contains a new line, the program will work if it is just one line. Why is it failing when there is a new line in the data? I get the error index out of range.

def highlight(data):
    for word in data.split(" "):
        if word[0] == "#":
            print "<FONT COLOR=\"brown\">" + word + "</FONT>",
        else:
            print word,

highlight("""hello world this
    is a #test that i am #writing.""")
Это было полезно?

Решение

This code will work:

def highlight(data):
    for word in data.split():
        if word[0] == "#":
            print "<FONT COLOR=\"brown\">" + word + "</FONT>",
        else:
            print word,

highlight("""hello world this
    is a #test that i am #writing.""")

This will split the text up by newlines and by whitespace.

Другие советы

Because a newline will make data.split(" ") contain ''s. You're trying to get the first element of that, and, well:

In [4]: ''[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-6f70a0cbdc74> in <module>()
----> 1 [][0]

IndexError: list index out of range

In [6]: a = """
   ...: hello world this
   ...:     is a #test that i am #writing."""

In [7]: a.split(' ')
Out[7]:
['\nhello',
 'world',
 'this\n',
 '',
 '',
 '',
 'is',
 'a',
 '#test',
 'that',
 'i',
 'am',
 '#writing.']

Just change it to data.split() and you'll be fine.

At the beginning of the second line of the tweet, there are four whitespaces.

"""test
    other_test""" == "test\n    other_test"

So if you split that string by a whitespace, you will get three empty strings.

>>> "test\n    other_test".split(" ")
['test\n', '', '', '', 'other_test']

Now if you try to access the first character of the string '', the character index is out of range.

To prevent this error, either use data.split() or check if the current string is empty.

Make sure that you have a "word" first:

def highlight(data):
    for word in data.split(" "):
        if word and word[0] == "#":
            print "<FONT COLOR=\"brown\">" + word + "</FONT>",
        else:
            print word,

In the future when asking, it helps to include the full text of your error message.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top