What should be done after getting this error : AttributeError: 'int' object has no attribute 'lower'
What should be done after getting this error : AttributeError: 'int' object has no attribute 'lower'
def tokenizer(text):
text = text.lower()
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
如何解决这个错误?文中整数值怎么办?
下面是需要分词的文字
14\n item 8.item 7. management's discussion and analysis of financial condition and results\n of operation\n\n\nyear ended december 28, 1997 compared to the year ended december 29, 1996\n\n\n in november 1996, the company initiated a major restructuring and growth\nplan designed to substantially reduce its cost structure and grow the business\nin order to restore higher levels of profitability for the company. by july\n1997, the company completed the major phases of the restructuring plan. the\n5.0 million of annualized cost savings anticipated from the restructuring\nresults primarily from the consolidation of administrative functions within the\ncompany, the rationalization of manufacturing and warehouse facilities\n(including a reduction in the number of production facilities from 26 to 8 and\nwarehouses from 61 to 18), the elimination of over 6,000 positions
您可以使用 str()
将文本值转换为字符串
代码如下
def tokenizer(text):
text = str(text).lower()
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
您收到此错误是因为 python 将数字处理为 int
而 int
class 没有函数 .lower()
所以首先您需要将此整数转换为字符串,以便能够使用 class str
中的函数,例如 .lower()
您可能正在将一个整数传递给 tokenizer(text)
。您所要做的就是使用 str(text).lower()
.
将 text
转换为字符串
您确定 'text' 参数是字符串吗?我觉得如果text变量的类型是int应该会出现这种错误...调试一下或者试试看是否真的是String:
def tokenizer(text):
if isinstance(text, str):
text = text.lower()
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
else:
ValueError('Sorry...')
这有效 -> '7'.lower()
def tokenizer(text):
text = text.lower()
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
如何解决这个错误?文中整数值怎么办?
下面是需要分词的文字
14\n item 8.item 7. management's discussion and analysis of financial condition and results\n of operation\n\n\nyear ended december 28, 1997 compared to the year ended december 29, 1996\n\n\n in november 1996, the company initiated a major restructuring and growth\nplan designed to substantially reduce its cost structure and grow the business\nin order to restore higher levels of profitability for the company. by july\n1997, the company completed the major phases of the restructuring plan. the\n5.0 million of annualized cost savings anticipated from the restructuring\nresults primarily from the consolidation of administrative functions within the\ncompany, the rationalization of manufacturing and warehouse facilities\n(including a reduction in the number of production facilities from 26 to 8 and\nwarehouses from 61 to 18), the elimination of over 6,000 positions
您可以使用 str()
将文本值转换为字符串
代码如下
def tokenizer(text):
text = str(text).lower()
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
您收到此错误是因为 python 将数字处理为 int
而 int
class 没有函数 .lower()
所以首先您需要将此整数转换为字符串,以便能够使用 class str
中的函数,例如 .lower()
您可能正在将一个整数传递给 tokenizer(text)
。您所要做的就是使用 str(text).lower()
.
text
转换为字符串
您确定 'text' 参数是字符串吗?我觉得如果text变量的类型是int应该会出现这种错误...调试一下或者试试看是否真的是String:
def tokenizer(text):
if isinstance(text, str):
text = text.lower()
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
else:
ValueError('Sorry...')
这有效 -> '7'.lower()