79294560

Date: 2024-12-19 14:10:47
Score: 1
Natty:
Report link

I get an error telling me there are no data to split:

It's better to show the actual result, e.g., the error is IndexError Exception like:

>>> empty_str = ""
>>> country_abbreviation = empty_str.strip().split()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

If this is the case, then we can say that the text got from columns[2].text.strip() is empty.

This may be the case you're reading from one column of a table and one row contains empty string.

To avoid this Exception, you should handle this empty-string case:

def get_first_word(column_data):
    if not isinstance(column_data, str):
        raise ValueError("column_data should be str")
    if not column_data.strip():
        return ""  # data is empty, return empty also
    return column_data.split(" ")[0]

And pass columns[2].text as argument to get_first_word to get the first word (if any).

>>> first_word_only = get_first_word(columns[2].text)
Reasons:
  • RegEx Blacklisted phrase (1): I get an error
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: wu qi