unsupported operand type(s) for %: 'type' and 'int'
时间: 2023-07-31 22:10:20 浏览: 44
这个错误通常是由于格式化字符串时,格式化符号(%)后面的数据类型与传入的实际数据类型不匹配导致的。比如你可能在格式化字符串时使用了%d格式化符号,但是传入的实际数据类型是一个字符串,这就会导致这个错误的出现。你可以检查一下你的代码中涉及到格式化字符串的部分,看看是否有这样的问题。如果有的话,可以尝试将格式化符号更改为与实际数据类型匹配的符号。
相关问题
unsupported operand type(s) for %: 'CellId' and 'int'
抱歉,之前的代码中出现了错误。在记录 `CELL_ID` 属性时,`CellId` 对象不能直接转换为字符串。您可以使用 `id().id()` 方法获取单元格的 ID,并将其转换为字符串类型。
以下是修正后的示例代码:
```python
import shapefile
from s2sphere import CellId, LatLngRect, LatLng, S2RegionCoverer
min_lat = 40.0 # 最小纬度
max_lat = 41.0 # 最大纬度
min_lng = -74.0 # 最小经度
max_lng = -73.0 # 最大经度
shp_writer = shapefile.Writer("cells", shapeType=shapefile.POLYGON)
shp_writer.field("CELL_ID", "C", size=50)
rect = LatLngRect.from_point_pair(
LatLng.from_degrees(min_lat, min_lng),
LatLng.from_degrees(max_lat, max_lng)
)
coverer = S2RegionCoverer()
covering = coverer.get_covering(rect)
for cell_id in covering:
cell = CellId(cell_id).to_lat_lng_rect()
vertices = [
(cell.lo().lat().degrees, cell.lo().lng().degrees),
(cell.lo().lat().degrees, cell.hi().lng().degrees),
(cell.hi().lat().degrees, cell.hi().lng().degrees),
(cell.hi().lat().degrees, cell.lo().lng().degrees),
]
shp_writer.poly(parts=[vertices])
shp_writer.record(str(cell_id.id()))
shp_writer.save("cells")
```
现在,使用 `cell_id.id()` 方法将 `CellId` 对象转换为整数 ID,并将其转换为字符串类型以进行记录。
再次抱歉造成的困扰,并希望以上修复能够解决问题。如果您还有其他问题,请随时提问!
unsupported operand type(s) for %: 'list' and 'int'
这个错误通常是因为你试图将一个整数与一个列表相结合使用百分号操作符(%)。请确保你在使用百分号操作符之前,将整数转换为字符串。例如,如果你想将一个整数插入到一个字符串中,你可以这样做:
```
my_list = ['a', 'b', 'c']
my_int = 1
my_string = 'The integer is: %s' % str(my_int)
my_list.append(my_string)
```
在这个例子中,我们首先将整数转换为字符串,然后将其插入到字符串中。最后,我们将字符串添加到列表中。请注意,我们使用了 str() 函数将整数转换为字符串,这是关键步骤。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)