fake = data["评论内容"] content = [] for sentence in fake: string = re.sub('\W*','',sentence) content.append(string)报出expected string or bytes-like object错误
时间: 2024-03-18 21:39:37 浏览: 122
这个错误通常是因为在对一个非字符串对象进行正则表达式操作时引起的。可能是数据中有某些列不是字符串类型,需要先对数据进行类型转换。你可以尝试使用`str()`函数将数据转换为字符串类型,然后再进行正则表达式操作,如下所示:
```
fake = data["评论内容"]
content = []
for sentence in fake:
string = re.sub('\W*','',str(sentence))
content.append(string)
```
这样应该就不会再报出这个错误了。
相关问题
fake = model(data) data, label, fake = [x*0.5+0.5 for x in [data, label, fake]]
This code snippet appears to be scaling the values in the variables data, label, and fake by a factor of 0.5 and then adding 0.5 to the result.
This is likely being done to normalize the data values to a range between 0 and 1. Scaling the values by 0.5 will reduce their magnitude by half, and adding 0.5 will shift the range up so that the minimum value becomes 0 and the maximum value becomes 1.
It's worth noting that without further context or information about the data and model being used, it's difficult to say for sure why this normalization is being done or whether it's appropriate.
fake = data["评论内容"] ### 文本长度 F8 = [] list1 = [] for sentences in fake: num = len(sentences) list1.append(num) object of type 'float' has no len()错误 if num > 50: length = 1 else: length = 0 F8.append(length)报出
这个错误的原因是,您的列表 "fake" 可能包含了不是字符串类型的元素,导致在计算字符串长度时出现了类型错误。
可以在计算字符串长度前,先用 `isinstance()` 函数判断一下元素是否是字符串类型,如果不是字符串类型,就跳过这个元素,不进行计算。修改代码如下:
```
fake = data["评论内容"]
F8 = []
for sentence in fake:
if isinstance(sentence, str):
if len(sentence) > 50:
F8.append(1)
else:
F8.append(0)
```
这样修改后,如果元素不是字符串类型,就会被跳过,不进行计算,也就不会出现类型错误了。
阅读全文