'int' object has no attribute 'to_token'
时间: 2023-07-31 13:09:43 浏览: 54
抱歉,我之前给出的代码有一个错误。在S2库中,`cell.id()`返回的是一个整数类型,因此没有`to_token()`方法。如果您想要将cell的ID转换为字符串类型,请使用`id().to_token()`方法。以下是正确的代码示例:
```python
w = shapefile.Writer("cells") # 创建shapefile写入器
w.field("CellID") # 添加字段
for cell in cells:
cell_id = str(cell.id().to_token()) # 获取cell的ID并转换为字符串类型
vertices = []
for i in range(0, 4):
vertex = cell.get_vertex(i)
vertices.append([vertex.lng().degrees, vertex.lat().degrees])
w.poly([vertices]) # 添加多边形
w.record(cell_id) # 添加记录
w.close() # 关闭写入器
```
这样,您就可以正确地将cell的ID转换为字符串类型,并将其添加到shapefile中。非常抱歉给您带来困扰,希望这次能够帮到您!
相关问题
object has no attribute __name__
"object has no attribute __name__"是Python中的一个错误提示,通常出现在使用装饰器时。装饰器是Python中一个非常有用的特性,它可以在不改变原函数代码的情况下,为函数添加额外的功能。但是,如果装饰器没有正确地处理被装饰函数的元信息,就会导致出现"object has no attribute __name__"的错误。
这个错误提示的意思是,Python解释器在尝试访问一个对象的__name__属性时,发现该对象没有这个属性。在Python中,__name__是一个特殊的属性,用于表示函数、类、模块等对象的名称。如果一个函数没有__name__属性,就会导致出现"object has no attribute __name__"的错误。
为了解决这个问题,可以尝试以下几种方法:
1. 确保装饰器正确地处理了被装饰函数的元信息,特别是__name__属性。
2. 尝试使用其他的装饰器,或者手动添加__name__属性。
3. 检查代码中是否有其他错误,例如拼写错误或语法错误,这些错误可能会导致出现"object has no attribute __name__"的错误。
Series object has no attribute to_numpy
1. 代码如下:
```python
print(wine.data)
print(wine.target)
```
2. 代码如下:
```python
print(wine.feature_names)
print(wine.target_names)
```
3. 代码如下:
```python
import pandas as pd
wine_data = pd.DataFrame(wine.data, columns=wine.feature_names)
pr