简体   繁体   中英

Why does a string gets split up into single characters with ' and , delimeters when using Folium heatmap generation?

I'm currently trying to use the Python and Folium to generate a heatmap on Leaflet.js and I'm getting some weird results.

I read in the longitude and latitudes from a csv file and then try to generate 'heatmap entries' using Folium.

If I print the variables during execution I see the following (somewhat pseudocode):

print row[0] 
-1.44986889

However, when I call it like this:

heatmap_map = folium.Map(location=[51.67109, -1.28278], zoom_start=2)

print '[*] Reading in and parsing the CSV file.'
with open('geoloc.csv','rb') as f:
    reader = csv.reader(f)

    for row in reader:
        heatmap_map.add_children(plugins.HeatMap([row[0]]))

heatmap_map.save("/tmp/heatmap.html")

What gets written into the heatmap.html file looks like this:

 var heat_map_b207be06b0c945a39fc48ed43ff5493b = L.heatLayer(
    [['-', '1', '.', '4', '4', '9', '8', '6', '8', '8', '9']],

As you can see the string gets broken up and each and every character gets delimeted with '' and,.

I have absolutely no idea what causes this or how to get around it. Any ideas out there?

I still have no idea why it split the string - so if anyone can answer that part, please feel free to do that - but I managed to resolve the issue.

I changed the way I read the csv file, reading it into a list and then iterating in the Folium function.

The code looks like this now:

print '[*] Reading in and parsing the CSV file.'
with open('geoloc.csv','rb') as f:

    reader = csv.reader(f)
    geolist = list(reader)

f.close()

heatmap_map.add_children(plugins.HeatMap([[t[0], t[1]] for t in geolist]))

print '[*] Creating and saving heatmap.\n'
heatmap_map.save("/tmp/heatmap.html")

This seems to have done the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM