簡體   English   中英

如何從 csv 中刪除逗號和括號

[英]How to remove commas and parenthesis from a csv

I am parsing through a pdf and finding the names of locations (Cities and Countries) then finding the longitude and latitude of those locations and saving that information to a csv and using that csv to make a global heat map to see where the cities are located在 pdf 中。 我遇到的問題是我的緯度和經度以這種格式“(緯度,經度)”保存,它將它們保存在 csv 的單個列中。 我試圖擺脫逗號和括號,因此它將它們分成兩個單獨的列。 這是我目前正在使用的代碼:

doc = open("/home/hank/Work/plotting-named-entities-in-python/text.txt",encoding="utf8").read()
places = GeoText(doc) #Uncomment out if you want to do a text file
cities = list(places.cities)

geolocator = Nominatim(timeout=2)

lat_lon = []
for city in cities: 
    try:
        location = geolocator.geocode(city)
        if location:
            #print(location.latitude, location.longitude)
            lat_lon.append(location)
    except GeocoderTimedOut as e:
        print("Error: geocode failed on input %s with message %s"%
             (city, e))
pd.DataFrame(lat_lon).to_csv("/home/hank/Work/file.csv", header=None,index=False)

它在哪里解析文件,然后找到緯度和經度,然后最終將所有這些數據保存到 pdf。

您可以從列表中創建一個 pd.DataFrame :

lat = [i[0] for i in lat_lon]
lon = [i[1] for i in lat_lon]
data = {'lat': lat, 'lon': lon}
df_lat_lon = pd.DataFrame(data)
df_lat_lon.to_csv("/home/hank/Work/file.csv", header=None,index=False)

location 變量包含一個地理編碼 object。 文檔在這里

要從存儲在 object 中的字符串中獲取緯度和經度,請使用:

lat = location.latitude
long = location.longitude

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM