使用GeoJson时出现Python编译时错误

2024-05-19 11:02:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了下面的代码,它给出了一个错误。这是我的密码:

fg_population.add_child(folium.GeoJson(data=open('world.json', 'r', encoding='utf-8-sig'),
style_function=lambda x: {'fillColor':'green' if x['properties']['POP2005'] < 10000000
else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 else 'red'}))

然后我得到以下错误消息:

ValueError: Unhandled object <_io.TextIOWrapper name='world.json' mode='r' encoding='utf-8-sig'>


Tags: 代码addjson密码worldif错误properties
1条回答
网友
1楼 · 发布于 2024-05-19 11:02:28

查看the docs,不应该使用data=来打开文件。此外,一些格式化和拆分会有帮助:

the_world = open('world.json', 'r', encoding='utf-8-sig')
the_style = lambda x: {'fillColor':
                           'green' if x['properties']['POP2005'] < 10000000
                      else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000
                      else 'red'}
the_map = folium.GeoJson(the_world, style_function=the_style)
fg_population.add_child(the_map)

相关问题 更多 >

    热门问题