I am using this line below to extract text:
country_abbreviation = columns[2].text.strip() # Country abbreviation
The result I obtain is for example fr FRA
. I would like to extract only fr
, ignoring the second word FRA
.
I have tried using this method, but I get an error telling me there are no data to split:
country_abbreviation = columns[2].text.strip().split()[0] # Country abbreviation
Any idea where the issue might come from?
Clément Aubert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
The issue was indeed the fact that some answers were empty.
I used this workaround:
country_abbreviation = columns[2].text.strip() # Country abbreviation
first_word = country_abbreviation.split()[0] if country_abbreviation else "" # Extract first word
Clément Aubert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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)
wu qi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.