使用GeoJson时Python编译时错误

时间:2017-09-20 03:45:59

标签: python python-3.x

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

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'>

1 个答案:

答案 0 :(得分:2)

查看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)