【问题标题】:Python Folium map not displaying when certain column is read from CSV?从 CSV 读取某些列时,Python Folium 地图不显示?
【发布时间】:2019-01-13 19:02:08
【问题描述】:

我正在尝试为我的公司创建一个 Folium 地图,用于内部营销。地图按原样加载。但是,当我希望在弹出文本中显示某个列时,地图无法加载。代码如下所示:

#read csv and create dataframe
df = pd.read_csv(sheet_url, delimiter=",", header=0, encoding='latin-1', engine='python') 
#turn dataframe from dict into a list
csv = [list(x) for x in df.values]
#organize data into array
locations = []
for location in csv:
    coords = [location[0], location[1]]
    try:
        locations.append({'company' : location[2], 'country' : location[3], 'products' : location[3], 'potential' : location[4], 'method' : location[13], 'note' : location[14], 'hover': f"{location[3]}<br>{location[2]}", 'click': f"<a href={location[9]}>Website</a><br>{location[13]}<br>{location[14]}", 'coordinates' : coords})
    except:
        print("failed because of some error")

当我尝试将 location[14] 添加到弹出窗口时会出现问题,但它不会在此处抛出错误。 将标记添加到地图的迭代也不会引发错误:

for x in locations:
    try:
        if x['potential'] == "Current Customer":
            folium.Marker(location = (x['coordinates']), popup = x['click'], tooltip = x['hover'], icon=folium.Icon(color='green')).add_to(current_cluster)
        else:
            folium.Marker(location = (x['coordinates']), popup = x['click'], tooltip = x['hover'], icon=folium.Icon(color='red')).add_to(potential_cluster)
    except:
        print("failed")

标记为“note”的列是我遇到问题的列。

到目前为止我已经尝试过:

  1. 使用' '.join(map(str, location[14])),同时遍历 csv。地图加载,文本在弹出窗口中,但由空格分隔 (L i k e t h i s)。
  2. 将分隔符从, 更改为其他任何内容,但会导致解析器错误。每行使用了很多不同的字符,但我不确定分隔符是否存在问题。
  3. 在 Chrome 上,调试器显示 Uncaught SyntaxError: Octal escape sequences are not allowed in template strings.

我认为在向地图添加标记时会出现问题?但是没有抛出任何错误,如果我在弹出窗口中包含“注释”,则地图将不会加载。任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: python pandas csv folium


    【解决方案1】:

    这可能是由于字符串中的特殊字符造成的。我有两个想法:

    • 确保您拥有最新版本的 folium (0.7.0),此行为已得到改进。
    • 在传递之前尝试转义文本:html.escape(my_string)

    你能分享location[14].note的确切值吗?这肯定有助于调试。此外,使用浏览器的开发者工具在控制台中查看错误消息。

    【讨论】:

    • Folium 是最新的。转义 HTML 并没有解决它。当我检查浏览器的调试器时,它显示 -----'Uncaught SyntaxError: Octal escape sequences are not allowed in template strings。'
    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多