使用 Geopy 和大数据进行地理编码

Geocoding with Geopy and big data

我有这个 CSV 文件,我正在用这个 python 脚本

    import csv
from geopy.geocoders import OpenCage

geolocator = OpenCage() #here some parameters are needed
with open('/Users/Ian/Desktop/Test02/151213_2015_martyrs_year_filtered_areas.csv', 'rb') as csvinput:
    with open('151213_locations_filtered_opencage.csv', 'w') as csvoutput:
       output_fieldnames = ['Province','Area', 'Country','Latitude','Longitude']
       writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames)
       reader = csv.DictReader(csvinput)
       for row in reader:
            #here you have to replace the dict item by your csv column names
            query = ','.join(str(x) for x in (row['Country'],row['Province'],row['Area']))
            print(query)

            try:
                address, (latitude, longitude) = geolocator.geocode(query)

            except:
                latitude = 'N/A'
                longitude = 'N/A'

            #here is the writing section
            output_row = {}
            output_row['Province'] = row['Province']
            output_row['Area'] = row['Area']
            output_row['Country'] = row['Country']
            output_row['Latitude'] = latitude
            output_row['Longitude'] = longitude
            writer.writerow(output_row)

CSV 文件摘录

Province,Area,Country
Aleppo,Jarablus,Syria
Aleppo,,Syria
Aleppo,Kafrnaya,Syria
Aleppo,Bazzaa,Syria
Aleppo,Anadan,Syria
Aleppo,Salheen,Syria
Aleppo,Kafrahamreh,Syria
Aleppo,Batbo,Syria
Aleppo,Hulluk,Syria
Aleppo,Sukkari neighborhood,Syria
Aleppo,"Ein Arab ""Kobani"": Srain",Syria
Aleppo,Bab,Syria
Aleppo,Tel Rifaat,Syria
Aleppo,Izaz,Syria
Aleppo,Sfaireh,Syria
Aleppo,Hritan,Syria
Aleppo,Manbej,Syria
Aleppo,Karm Bitar,Syria
Aleppo,Atarib,Syria
Aleppo,Neirab,Syria
Aleppo,Afrin: Mariameen,Syria
Aleppo,Zerbeih Town,Syria
Aleppo,Sakhour,Syria
Aleppo,Tal Abad,Syria
Aleppo,Tal Arran,Syria
Aleppo,Talqrah,Syria
Aleppo,Tqad,Syria
Aleppo,Darat Azza,Syria
Aleppo,Bayanoun,Syria
Aleppo,Misakan,Syria
Aleppo,Qosaileh,Syria
Aleppo,Mashatieh neighborhood,Syria
Aleppo,Kabbasin,Syria
Aleppo,Harjalleih,Syria
Aleppo,Bab Road neighborhood,Syria
Aleppo,Qanater village,Syria
Aleppo,Maskaneh,Syria
Aleppo,Jeb el-Qibba,Syria
Aleppo,Hwer al-Aes,Syria
Aleppo,"Ein Arab""Kobani""",Syria
Aleppo,Anjareh,Syria
Aleppo,Hayyan,Syria
Aleppo,Sheikh Saeed,Syria
Aleppo,Liramon,Syria
Aleppo,Karam Muayassar,Syria
Aleppo,Tel Daman: Tel Maseh village,Syria
Aleppo,Eibeen Simon,Syria
Aleppo,Deir Hafir,Syria
Aleppo,Sheikh Ali village,Syria
Aleppo,Afrin,Syria
Aleppo,Khan Toman,Syria
Aleppo,Rasm al-Aboud village,Syria
Aleppo,Hulluk: Ein el-Tal,Syria
Aleppo,Khan el-Asal,Syria
Aleppo,Alshyoukh Alfoqani,Syria
Aleppo,Mare,Syria
Aleppo,Der Jamal,Syria
Aleppo,Mkahleh,Syria
Aleppo,Zebdieh,Syria
Aleppo,Ansari,Syria
Aleppo,Bustan el-Qassr,Syria
Aleppo,Eastern Ansari,Syria
Aleppo,Banjaira,Syria
Aleppo,Kallaseh,Syria
Aleppo,Tal Memo,Syria
Aleppo,Museijrat al-Bosheikh,Syria
Aleppo,Aziziah,Syria
Aleppo,Salahiddin,Syria
Aleppo,Tel Rifaat: Herpel,Syria
Aleppo,Shokaileh,Syria
Aleppo,Kafrobeid,Syria
Aleppo,Jazraya,Syria
Aleppo,Fardous,Syria
Aleppo,Mestaha,Syria
Aleppo,Bab Neirab,Syria
Aleppo,Magaier,Syria
Aleppo,Karam el-Nuzha,Syria
Aleppo,Tal Hdia,Syria
Aleppo,Talhia,Syria
Aleppo,Karam Al-Tarrab,Syria
Aleppo,Zerbeih town,Syria
Aleppo,Tel Daman,Syria
Aleppo,Great Orme,Syria
Aleppo,Ritian,Syria
Aleppo,Barkoum,Syria
Aleppo,Sheikh Maksoud,Syria
Aleppo,Ihres,Syria
Aleppo,Sfaireh: Azzan mountain,Syria
Aleppo,Hader village,Syria
Aleppo,Mraij,Syria
Aleppo,Deir Hafir: Tel Ayoub,Syria
Aleppo,Abtin village,Syria
Aleppo,Kafrnoran,Syria
Aleppo,Deir Hafir: Sheikh Ahmed village,Syria
Aleppo,Rasm al-Sheikh,Syria
Aleppo,Ezaa Neighborhood,Syria
Aleppo,Zitan,Syria
Aleppo,Kfarkarmin village,Syria
Aleppo,Blas,Syria
Aleppo,Seif El-Dawla,Syria
Aleppo,Bab el-Faraj,Syria
Aleppo,Teljabin,Syria
Aleppo,Rjaileh,Syria
Aleppo,Kafra,Syria
Aleppo,Ziyareh,Syria
Aleppo,Sfaireh: Mgairat Shibli village,Syria
Aleppo,Talaarn,Syria
Aleppo,Sahareh,Syria
Aleppo,Shaar,Syria
Aleppo,Meng,Syria
Aleppo,Mowasalat,Syria
Aleppo,Qadi Askar,Syria
Aleppo,Mashad,Syria
Aleppo,Izaz: Kfarkplpin,Syria
Aleppo,Rashideen,Syria
Aleppo,Marja,Syria
Aleppo,Kafin,Syria
Aleppo,Dabik,Syria
Aleppo,Suleiman al-Halabi,Syria
Aleppo,Mayer,Syria
Aleppo,Tlaleen,Syria
Aleppo,Hamdania,Syria
Aleppo,Tel Tokan,Syria
Aleppo,Tel Shgaib,Syria
Aleppo,Tadif,Syria
Aleppo,Manbei: Deir Jamal,Syria
Aleppo,Qubtan el-Jabal,Syria
Aleppo,Ahtimlat,Syria
Aleppo,Majas Village,Syria
Aleppo,Masaken al-Sabeel,Syria
Aleppo,Sheikh Khader,Syria
Aleppo,Masharq,Syria
Aleppo,Bshentreh village,Syria
Aleppo,Hur village,Syria
Aleppo,Ein Arab: Srain,Syria
Aleppo,Amerieh,Syria
Aleppo,Safira: Talaran,Syria
Aleppo,Jaina,Syria
Aleppo,Ashrafieh,Syria
Aleppo,Qustal Harami,Syria
Aleppo,Marestit el-Khan,Syria
Aleppo,Hamidiya village,Syria
Aleppo,Muyassar neighborhood,Syria
Aleppo,Zyade village,Syria
Aleppo,Bab: Braij Palace village,Syria
Aleppo,Um al-Hosh village,Syria
Aleppo,Darat Azza: Mount Simon: Deir Simeon Village,Syria
Aleppo,Arran,Syria
Aleppo,Shwehneh Mountain,Syria
Aleppo,Masakin Hanano,Syria
Aleppo,Kafrdaal,Syria
Aleppo,Hoteh,Syria
Aleppo,Rabiea,Syria
Aleppo,bab,Syria
Aleppo,Karam el-Baik neighborhood,Syria
Aleppo,Madaein Kabira,Syria
Aleppo,Maadi neighborhood,Syria
Aleppo,Jamiat al-Zahraa,Syria
Aleppo,Maarat Artik,Syria
Aleppo,Karm Al-Afandi,Syria
Aleppo,New Aleppo,Syria
Aleppo,Deir Hafir: Rasm Harmel,Syria
Aleppo,"Ein Arab ""Kobani""",Syria
Aleppo,Absmo,Syria
Aleppo,Deir Hassan,Syria
Aleppo,Talkrah,Syria
Aleppo,Simon Mount: Mansoura,Syria
Aleppo,Atarib: Twama,Syria
Aleppo,Kafarnaha,Syria
Aleppo,Bab Qinnasrin,Syria
Aleppo,Pedro Mount,Syria
Aleppo,Dahrat Awad,Syria
Aleppo,Qirliq neighborhood,Syria
Aleppo,Um Hosh,Syria
Aleppo,Baideen,Syria
Aleppo,Akhtareen,Syria
Aleppo,Izaz: Soran town,Syria
Aleppo,Harableh,Syria
Aleppo,Bab: Abu Taltal village,Syria
Aleppo,Bareh Azzawi,Syria
Aleppo,Malha,Syria
Aleppo,Ezaa neighborhood,Syria
Aleppo,Kafernasaeh,Syria
Aleppo,Karam el-Jabal,Syria
Aleppo,Tadeel,Syria
Aleppo,Abo Mjaher,Syria
Aleppo,al-Aes,Syria
Aleppo,khalsa,Syria
Aleppo,Arjileh,Syria
Aleppo,Alas,Syria
Aleppo,Khalsa,Syria
Aleppo,Jisr el-Hajj,Syria
Aleppo,Qataneh,Syria
Aleppo,Ahmadia,Syria
Aleppo,Garliq,Syria
Aleppo,Sheikh Najjar,Syria

但是原始的 csv 看起来像这样

rowid,Name,Status,Sex,Province,Area,Date_of_Death,Cause_of_Death
1,Hasan Ali Saad al-Hareeri ,Civilian,Adult - Male,Daraa,Sawara,2015-12-10,Warplane shelling
2,Riham Adnan Swais ,Civilian,Adult - Female,Homs,Telbeiseh,2015-12-10,Shelling
3,Ammar Ahmad al-Njoam ,Civilian,Adult - Male,Aleppo,Jarablus,2015-12-10,Explosion
4,Yaseen Slaiman al-Salkhadi ,Civilian,Adult - Male,Daraa,Jassim,2015-12-10,Warplane shelling
5,Ayman Noman Qanatra ,Civilian,Adult - Male,Daraa,Jassim,2015-12-10,Warplane shelling
6,Abdul Kareem Mohammad al-Tamki ,Civilian,Adult - Male,Daraa,Sheikh Miskeen,2015-12-10,Warplane shelling
7,Ahmad Mohammad al-Tamki ,Civilian,Child - Male,Daraa,Sheikh Miskeen,2015-12-10,Warplane shelling
8,Hamze Mohammad al-Tamki ,Civilian,Adult - Male,Daraa,Sheikh Miskeen,2015-12-10,Warplane shelling
9,Mohammad Ahmad al-Khaleel ,Non-Civilian,Adult - Male,Daraa,Sheikh Miskeen,2015-12-10,Shelling
10,Abdul Kareem Abdul Lateef al-Mohammad ,Non-Civilian,Adult - Male,Hama,Lattamna,2015-12-10,Shooting
11,Mahdi Basem Qatteni ,Non-Civilian,Adult - Male,Idlib,Khan Sheikhoun,2015-12-10,Shooting
12,Ahmad Ibraheem al-Sayed Omar ,Civilian,Adult - Male,Aleppo,,2015-12-10,Warplane shelling
13,Unidentified ,Civilian,Child - Male,Aleppo,Kafrnaya,2015-12-10,Warplane shelling
14,Unidentified 1 ,Civilian,Adult - Female,Aleppo,Bazzaa,2015-12-10,Warplane shelling
15,Unidentified 2 ,Civilian,Child - Male,Aleppo,Bazzaa,2015-12-10,Warplane shelling
16,Unidentified 3 ,Civilian,Child - Male,Aleppo,Bazzaa,2015-12-10,Warplane shelling
17,Mohammad Ahmad al-Farhoud ,Civilian,Adult - Male,Idlib,Telminis,2015-12-10,Shooting
18,Yousef al-Saleh ,Non-Civilian,Adult - Male,Idlib,Kafroumeh,2015-12-10,Shooting
19,Basel Hsain al-Knoah ,Non-Civilian,Adult - Male,Idlib,Kafroumeh,2015-12-10,Shooting
20,Basel Abdul Rahman Qintareh ,Non-Civilian,Adult - Male,Idlib,Kafroumeh,2015-12-10,Shooting
21,Hani Khairallah Jbaneej ,Non-Civilian,Adult - Male,Idlib,Kafroumeh,2015-12-10,Shooting
22,Ahmad Mohammad al-Shartah ,Non-Civilian,Adult - Male,Idlib,Kafroumeh,2015-12-10,Shooting
23,wife of Faisal al-Ahmad al-Khaleefe ,Civilian,Adult - Female,Deir Ezzor,Qourieh,2015-12-10,Warplane shelling
24,Unidentified ,Civilian,Adult - Male,Deir Ezzor,Takaya,2015-12-10,Kidnapping - Execution
25,Unidentified 1 ,Civilian,Adult - Male,Deir Ezzor,Tabya Jazera village,2015-12-10,Kidnapping - Execution
26,Unidentified 2 ,Civilian,Adult - Male,Deir Ezzor,Tabya Jazera village,2015-12-10,Kidnapping - Execution
27,Unidentified 3 ,Civilian,Adult - Male,Deir Ezzor,Tabya Jazera village,2015-12-10,Kidnapping - Execution
28,Unidentified 4 ,Civilian,Adult - Male,Deir Ezzor,Tabya Jazera village,2015-12-10,Kidnapping - Execution
29,Unidentified 5 ,Civilian,Adult - Male,Deir Ezzor,Tabya Jazera village,2015-12-10,Kidnapping - Execution
30,Fayez Qabaqji ,Non-Civilian,Adult - Male,Lattakia,Turkmen mount,2015-12-10,Shooting
31,Fahed al-Asali ,Non-Civilian,Adult - Male,Damascus,Jobar,2015-12-09,Shooting
32,Amoun Murad ,Civilian,Adult - Female,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
33,Kafa Mezweq ,Civilian,Adult - Female,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
34,Hala Mohammad ,Civilian,Adult - Female,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
35,Ahmad Mezweq ,Civilian,Child - Male,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
36,Ahlam Mezweq ,Civilian,Child - Female,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
37,Adnan Hussain Haj Kheder ,Civilian,Child - Male,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
38,Wife of Hussain Haj Kheder ,Civilian,Adult - Female,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
39,daughter of Hussain Haj Kheder 1 ,Civilian,Child - Female,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
40,Muhammad Muhammad al-Aoutani ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
41,Ibrahim al-Shami ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
42,Tahsin al-Durra ,Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-09,Warplane shelling
43,Samer Safaya ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
44,Muhammad al-Qassir ,Civilian,Child - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
45,Ibtisam Abdlunaser al-Khatib ,Civilian,Child - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
46,Tuqa Saria al-Zein ,Civilian,Child - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
47,Hadi Darwish ,Civilian,Child - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
48,Ghalia Abdulnaser al-Khatib ,Civilian,Child - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
49,Yamen Amin Abbas ,Civilian,Child - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
50,Muhammad Wasim al-Kharboutli ,Civilian,Adult - Male,Damascus Suburbs,Mleha,2015-12-09,Warplane shelling
51,Muhammad Ismael al-Khatib ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
52,Ismael Saeid al-Khatib ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
53,Saeid Abd al-Jawad ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
54,Abdulmoein al-Zein ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
55,Unidentified 1 ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
56,Unidentified 2 ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
57,Unidentified 3 ,Civilian,Adult - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
58,Mohammd Omar al-Deirani ,Non-Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-09,Shelling
59,daughter of Hussain Haj Kheder 2 ,Civilian,Child - Female,Idlib,Jisr Shagour: Mraand,2015-12-09,Warplane shelling
60,Ramez Shaher Qadour ,Civilian,Child - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
61,Yazan Shaher Qadour ,Civilian,Child - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
62,Sham Ahmad Qadour ,Civilian,Child - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
63,Ibraheem Ahmad Qadour ,Civilian,Child - Male,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
64,Fatemah Qadour ,Civilian,Adult - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
65,Houriah al-Aqra ,Civilian,Adult - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
66,Shahed al-Yousef ,Civilian,Child - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
67,Faridah Qalla ,Civilian,Adult - Female,Damascus Suburbs,Hamouria,2015-12-09,Warplane shelling
68,Abo Eisa ,Non-Civilian,Adult - Male,Damascus,Yarmuk Camp,2015-12-09,Shooting
69,Mohammad Ataya Hadeideh ,Civilian,Adult - Male,Damascus Suburbs,Saqba,2015-12-09,Warplane shelling
70,Mohammad Abdullah Ali Raslan ,Non-Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-09,Shooting
71,Ahmad Khaled al-Ayoub ,Non-Civilian,Adult - Male,Idlib,Al-Tih town,2015-12-09,Shooting
72,Salem Abo Saeid ,Non-Civilian,Adult - Male,Idlib,Saraman,2015-12-09,Shooting
73,Muneeb Abdul Baset al-Shawarghi ,Non-Civilian,Adult - Male,Idlib,Maarshamsha,2015-12-09,Shooting
74,Mohammad Ahmad al-Alayan ,Civilian,Adult - Male,Hama,Kafrzita,2015-12-09,Warplane shelling
75,Bahaa Eddin al-Hamwi ,Non-Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-09,Shooting
76,Saeid Smoad ,Non-Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-09,Shooting
77,Basheer Mahmoud Shihab ,Non-Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-09,Shooting
78,Unidentified ,Civilian,Adult - Male,Homs,Palmyra,2015-12-09,Warplane shelling
79,Ahmad Hamze Layla ,Civilian,Child - Male,Aleppo,Anadan,2015-12-09,Warplane shelling
80,Abdul Wahhab Hsain ,Civilian,Adult - Male,Idlib,Jisr Shagour: Najieh,2015-12-08,Warplane shelling
81,Khaled Khalaf al-Zubi ,Civilian,Adult - Male,Daraa,Jassim,2015-12-08,Warplane shelling
82,"Hameede Hamed al-Hariri ""al-Haishan"" ",Civilian,Adult - Female,Daraa,Sheikh Miskeen,2015-12-08,Shelling
83,Ziyad Sayel al-Mreheel ,Non-Civilian,Adult - Male,Daraa,Dael,2015-12-08,Shooting
84,Ahmad Abdul Ilah al-Dairi ,Civilian,Adult - Male,Daraa,Sheikh Miskeen,2015-12-08,Detention - Torture
85,Noah Jaqmour ,Non-Civilian,Adult - Male,Idlib,Ariha,2015-12-08,Shooting
86,Mahmoud Beshe ,Civilian,Child - Male,Aleppo,Salheen,2015-12-08,Warplane shelling
87,Omar Sbainati ,Civilian,Child - Male,Damascus Suburbs,Marj,2015-12-08,Warplane shelling
88,Meryam al-Jassem ,Civilian,Adult - Female,Damascus Suburbs,Marj,2015-12-08,Warplane shelling
89,Unidentified ,Civilian,Adult - Female,Hama,Telool al-Hemer,2015-12-08,Warplane shelling
90,Hatem Uday Akashe ,Civilian,Child - Male,Daraa,Jassim,2015-12-08,Warplane shelling
91,Waddah Yaseen al-Ghazzawi ,Civilian,Adult - Male,Daraa,Sheikh Miskeen,2015-12-08,Detention - Torture
92,Mohammad Sami al-Mahmoud ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
93,Haloum al-Aas ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
94,Maroush Abdo al-Mahmoud ,Civilian,Adult - Female,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
95,Samirah Abdo al-Mahmoud ,Civilian,Adult - Female,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
96,Sabah Sehreej ,Civilian,Adult - Female,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
97,Unidentified ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
98,Abduljalil al-Mahmoud ,Civilian,Child - Male,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
99,Hala al-Mahmoud ,Civilian,Child - Female,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
100,Yaser Yaser al-Mahmoud ,Civilian,Child - Male,Aleppo,Kafrahamreh,2015-12-08,Warplane shelling
101,Abdulraheem al-Hamoud ,Non-Civilian,Adult - Male,Aleppo,Batbo,2015-12-08,Shooting
102,Ahmad Hanouf ,Non-Civilian,Adult - Male,Lattakia,Jabla,2015-12-08,Shooting
103,Fadi Subhi al-Omar ,Non-Civilian,Adult - Male,Idlib,Binnish,2015-12-08,Shooting
104,Mohammad Nour Abdulla al-Aaraj ,Non-Civilian,Adult - Male,Idlib,Has,2015-12-08,Shooting
105,Mohammad Ibraheem al-Ali ,Non-Civilian,Adult - Male,Idlib,Kafrsijneh,2015-12-08,Shooting
106,Mulham Ahmad Qeetaz ,Non-Civilian,Adult - Male,Idlib,Maart Numan,2015-12-08,Shooting
107,Murad Mohammad Qeetaz ,Non-Civilian,Adult - Male,Idlib,Maart Numan,2015-12-08,Shooting
108,Abdul Hameed Musa al-Gharibi ,Non-Civilian,Adult - Male,Idlib,Kafrauaid,2015-12-08,Shooting
109,Mohammad Badran ,Civilian,Adult - Male,Daraa,Khirbet Ghazaleh,2015-12-08,Detention - Torture
110,Mohammad Natheir Darkal ,Civilian,Adult - Male,Damascus,Midan,2015-12-08,Detention - Torture
111,Unidentified ,Civilian,Adult - Female,Raqqa,Garimat Huzeima,2015-12-08,Warplane shelling
112,Unidentified ,Civilian,Child - Male,Damascus Suburbs,Madaya,2015-12-08,Other
113,Unidentified 1 ,Civilian,Adult - Male,Damascus,Erbeen,2015-12-08,Shelling
114,Unidentified 2 ,Civilian,Adult - Male,Damascus,Erbeen,2015-12-08,Shelling
115,Mohammad Ahmad Abd Rabuh ,Civilian,Child - Male,Damascus Suburbs,Hamouria,2015-12-08,Shelling
116,From al-Jebbah Family ,Civilian,Child - Male,Damascus Suburbs,Harasta,2015-12-08,Shelling
117,Ibraheem Saleh al-Khateeb ,Civilian,Adult - Male,Daraa,Enkhel,2015-12-08,Warplane shelling
118,Fatemah al-Jubah Ji ,Civilian,Adult - Female,Damascus Suburbs,Harasta,2015-12-08,Shelling
119,Ismael Khaled Batman al-Zgheer ,Civilian,Adult - Male,Homs,Palmyra,2015-12-08,Warplane shelling
120,Zaher al-Homsi ,Non-Civilian,Adult - Male,Hama,,2015-12-08,Shooting
121,Ahmad Hamdo al-Hallaq ,Non-Civilian,Adult - Male,Hama,Soran,2015-12-08,Shooting
122,Omar al-Hsain ,Non-Civilian,Adult - Male,Hama,,2015-12-08,Shooting
123,Mohammad Abdul Rahman Reslan ,Non-Civilian,Adult - Male,Aleppo,,2015-12-08,Shooting
124,Slaiman Shaghal ,Civilian,Child - Male,Aleppo,Anadan,2015-12-08,Warplane shelling
125,Yazan Hassan Hsain ,Non-Civilian,Adult - Male,Idlib,Kafryhmoul,2015-12-07,Shooting
126,Suleiman Waleed al-Ayanah ,Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-07,Shelling
127,Huda al-Aani ,Civilian,Adult - Female,Deir Ezzor,Ardi,2015-12-07,Warplane shelling
128,son of Huda al-Aani ,Civilian,Child - Male,Deir Ezzor,Ardi,2015-12-07,Warplane shelling
129,Khaled Konbel ,Non-Civilian,Adult - Male,Lattakia,Turkmen mount,2015-12-07,Shooting
130,Ahmad Abo al-Wared ,Non-Civilian,Adult - Male,Damascus Suburbs,Daraya,2015-12-07,Shooting
131,Omar Ali al-Ekrah ,Non-Civilian,Adult - Male,Damascus Suburbs,Douma,2015-12-07,Shooting
132,Mohammad Majed Kabarah ,Civilian,Child - Male,Damascus Suburbs,Zamalka,2015-12-07,Shelling
133,Om Jameel Kheito ,Civilian,Adult - Female,Damascus Suburbs,Zabadany,2015-12-07,Explosion
134,Omar Mustafa Jdeid ,Civilian,Adult - Male,Damascus Suburbs,Madaya,2015-12-07,Explosion
135,"Wael Mahmoud ""Istanbouli"" ",Civilian,Adult - Male,Damascus Suburbs,Madaya,2015-12-07,Explosion
136,Hasan Ahmad al-Khateeb ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-07,Warplane shelling
137,Abdulla Ramadan ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-07,Warplane shelling
138,Hamdo Ramadan ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-07,Warplane shelling
139,Raed Ramadan ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-07,Warplane shelling
140,Aamer Ramadan ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-07,Warplane shelling
141,Mohammad Salem al-Mahmoud ,Civilian,Adult - Male,Aleppo,Kafrahamreh,2015-12-07,Warplane shelling
142,Yehya Qasemo ,Non-Civilian,Adult - Male,Hama,Al-Sermenieh,2015-12-07,Shooting
143,Hasan Ahmad al-Hardan ,Civilian,Adult - Male,Aleppo,Salheen,2015-12-07,Warplane shelling
144,Ayda al-Hasan ,Civilian,Adult - Female,Aleppo,Salheen,2015-12-07,Warplane shelling
145,Sahar Hamwi ,Civilian,Adult - Female,Aleppo,Salheen,2015-12-07,Warplane shelling
146,Abdul Hameed Sultan ,Civilian,Adult - Male,Aleppo,Hulluk,2015-12-07,Warplane shelling
147,Bilal Mahmoud ,Civilian,Child - Male,Aleppo,Hulluk,2015-12-07,Warplane shelling
148,Sahar Ghazal al-Hamwi ,Civilian,Adult - Female,Aleppo,Hulluk,2015-12-07,Warplane shelling
149,Fatme Ahmad al-Jasem ,Civilian,Adult - Female,Aleppo,Hulluk,2015-12-07,Warplane shelling
150,Unidentified ,Civilian,Child - Male,Aleppo,Hulluk,2015-12-07,Warplane shelling
151,Hasan Mohammad Rammo ,Civilian,Adult - Male,Aleppo,Sukkari neighborhood,2015-12-07,Warplane shelling
152,Hajar Mahmoud Akeel ,Civilian,Child - Female,Aleppo,Sukkari neighborhood,2015-12-07,Warplane shelling
153,Shaymaa Mahmoud Akeel ,Civilian,Child - Female,Aleppo,Sukkari neighborhood,2015-12-07,Warplane shelling
154,Abed Seyam ,Non-Civilian,Adult - Male,Damascus,Yarmuk Camp,2015-12-07,Warplane shelling
155,Unidentified ,Civilian,Adult - Male,Aleppo,,2015-12-07,Shelling
156,Abdul Rawof Mohammad ,Non-Civilian,Adult - Male,Aleppo,,2015-12-07,Shooting
157,Waleed Hamoush ,Non-Civilian,Adult - Male,Aleppo,,2015-12-07,Shooting
158,Abdulkareem al-Suleiman ,Non-Civilian,Adult - Male,Aleppo,,2015-12-07,Shooting
159,Mohammad al-Qarrat ,Non-Civilian,Adult - Male,Aleppo,,2015-12-07,Shooting
160,Abdullah al-Melhem al-Abdullah ,Non-Civilian,Adult - Male,Aleppo,,2015-12-07,Shooting
161,Yaser Saeed Ghanoum ,Non-Civilian,Adult - Male,Aleppo,,2015-12-07,Shooting
162,Ali Sultan al-Samouri ,Non-Civilian,Adult - Male,Daraa,Jamleh,2015-12-07,Kidnapping - Execution
163,Yousef Sultan al-Samouri ,Non-Civilian,Adult - Male,Daraa,Jamleh,2015-12-07,Kidnapping - Execution
164,Rami al-Sayo ,Non-Civilian,Adult - Male,Deir Ezzor,,2015-12-07,Kidnapping - Execution
165,Akram al-Merie ,Civilian,Adult - Male,Deir Ezzor,,2015-12-07,Kidnapping - Execution
166,Adnan Saleh al-Shaqran ,Non-Civilian,Adult - Male,Daraa,Fuqia,2015-12-07,Shooting
167,Alaa Wael Sadaqa ,Non-Civilian,Adult - Male,Daraa,Fuqia,2015-12-07,Shooting
168,Oqba Jad al-Kareem al-Hareeri ,Non-Civilian,Adult - Male,Daraa,Fuqia,2015-12-07,Shooting
169,Ali Abdul Ghani al-Shaqran ,Non-Civilian,Adult - Male,Daraa,Fuqia,2015-12-07,Shooting
170,Bushra Ahmad Adeel al-Hareeri ,Civilian,Adult - Female,Daraa,Bassr Al-Hareer,2015-12-07,Warplane shelling
171,Hsain Sami Shawar ,Civilian,Adult - Male,Idlib,Jisr Shagour: Najieh,2015-12-07,Warplane shelling
172,wife of Hsain Sami Shawar ,Civilian,Adult - Female,Idlib,Jisr Shagour: Najieh,2015-12-07,Warplane shelling

这里有 15k+ 行,您可能会注意到某些地方多次出现相同的条目。我的工作流程是将数据从 sqlite 数据库中获取到这个大 csv 中。之后我过滤了双条目所以我只得到每个 place/province 一次。因此可以减少对地理编码器的查询(我天真地认为你根本没有限制)。我现在的问题是我有两个数据集,我无法将它们再次放在一起,因为简单的 "search and replace all entries" 和 latitude/longitude 信息仍然是一项困难且浪费时间的任务。也因为条目编号不匹配。 我有两个想法,对于这两个想法,我都不知道如何实现,因为我是 python 的新手,而且我整天都在处理这些数据。

  1. 自动执行搜索过程并将 (Province/Area) 替换为 (Province/Area,Latitude,Longitude)
  2. 每次我都有好几个人在同一天死在同一个地方。我可以从技术上总结它们,这样整个数据就会变得更小(我应该在哪里做。在 SQLite oder 中 Python 和 csv)。所以我可以将这些数据查询到地理编码器,这样我就有了一个完整的文件。像这样,我会向地理编码器查询省和地区:

     rowid,Value_by_People,Status,Province,Area,Date_of_Death,Cause_of_Death
    1,13,Civilian,Daraa,Sawara,2015-12-10,Warplane shelling
    2,50,Civilian,Daraa,Sawara,2015-12-7,Shooting
    

谢谢你的想法

编辑1

query = ','.join(str(x) for x in (row['Country'],row['Province'],row['Area']))
            errorquery = ','.join(str(x) for x in (row['Province'],row['Country']))
            print(query)

            try:
                address, (latitude, longitude) = geolocator.geocode(query)

            except:
                address, (latitude, longitude) = geolocator.geocode(errorquery)
                #latitude = 'N/A'
                #longitude = 'N/A'

编辑 3

我尝试了这些更改,但无法正常工作

import csv
from geopy.geocoders import GoogleV3

geolocator = GoogleV3() #here some parameters are needed
lookups = {} # declare this above the loop
global tempquery
global latitude, longitude

def lookup_location(query, errorquery, lookups):
    tempquery = query

    try:
        address, (latitude, longitude) = lookups[query]
    except KeyError: # new lookup
        try:
            #address, (latitude, longitude) = geolocator.geocode(query)
            address, (latitude, longitude) = lookups[errorquery]
        except: # should probably try to catch specific exceptions
            #address = latitude = longitude = None
            try:
                address, (latitude, longitude) = geolocator.geocode(query)
                #address, (latitude, longitude) = lookups[errorquery]
            except KeyError: # new lookup
                try:
                    address, (latitude, longitude) = geolocator.geocode(errorquery)
                    tempquery = errorquery
                except:
                    address = latitude = longitude = 'None'
                    tempquery = errorquery
            lookups[tempquery] = address, (latitude, longitude)
    return address, latitude, longitude


with open('151213_Martyrs_filtered_civilian_2015_with_country.csv', 'rb') as csvinput:
    with open('151213_locations_filtered_fiexedwith_cities_country.csv', 'w') as csvoutput:
       output_fieldnames = ['Name','Status','Sex','Country','Province','Area','Date_of_Death','Cause_of_Death','Latitude','Longitude']
       writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames)
       reader = csv.DictReader(csvinput)
       for row in reader:
            #here you have to replace the dict item by your csv column names
            query = ','.join(str(x) for x in (row['Country'],row['Province'],row['Area']))
            errorquery = ','.join(str(x) for x in (row['Country'],row['Province'],row['Province']))
            lookup_location(query, errorquery, lookups)


            output_row = {}
            output_row['Name'] = row['Name']
            output_row['Status'] = row['Status']
            output_row['Sex'] = row['Sex']
            output_row['Country'] = row['Country']
            output_row['Province'] = row['Province']
            output_row['Area'] = row['Area']
            output_row['Date_of_Death'] = row['Date_of_Death']
            output_row['Cause_of_Death'] = row['Cause_of_Death']
            output_row['Latitude'] = 0
            output_row['Longitude'] = 0
            writer.writerow(output_row)

有这些错误

Traceback (most recent call last):
  File "geopy_script_04_changes.py", line 43, in <module>
    lookup_location(query, errorquery, lookups)
  File "geopy_script_04_changes.py", line 21, in lookup_location
    address, (latitude, longitude) = geolocator.geocode(query)
TypeError: 'NoneType' object is not iterable

已更新以反映 OP 编辑​​: 我认为创建第二个 CSV 文件是不必要的。相反,您应该缓存已查找的位置。

lookups = {} # declare this above the loop

创建一个查找查询的函数,将接受正常查询或错误查询。将查询和查找字典作为参数传递给 lookup。函数returns地址,经纬度

def lookup_location(query, lookups):
    try:
        address, (latitude, longitude) = lookups[query]
    except KeyError: # new lookup
        try:
            address, (latitude, longitude) = geolocator.geocode(query)
        except: # should probably try to catch specific exceptions
            address = latitude = longitude = None
        lookups[query] = address, (latitude, longitude)
    return address, latitude, longitude

如果您超出查找配额,您可以使用 pickle 将查找保存到一个文件中,并在您的配额重置时恢复

from pickle import dumps, loads

with open("lookup data.pik", "wb") as fileobj:
    fileobj.write(dumps(lookups))

并加载它...

with open("lookup data.pik", "rb") as fileobj:
    lookups = loads(fileobj.read())

回复:编辑 3

这更符合我的想法

import csv
from geopy.geocoders import GoogleV3

geolocator = GoogleV3() #here some parameters are needed
lookups = {} # declare this above the loop
global tempquery
global latitude, longitude

def lookup_location(query, lookups):
    try:
        address, (latitude, longitude) = lookups[query]
    except KeyError: # new lookup
        try:
            address, (latitude, longitude) = geolocator.geocode(query)
        except: # should probably try to catch specific exceptions
            address = latitude = longitude = None
        lookups[query] = address, (latitude, longitude)
    return address, latitude, longitude


with open('151213_Martyrs_filtered_civilian_2015_with_country.csv', 'rb') as csvinput:
    with open('151213_locations_filtered_fiexedwith_cities_country.csv', 'w') as csvoutput:
       output_fieldnames = ['Name','Status','Sex','Country','Province','Area','Date_of_Death','Cause_of_Death','Latitude','Longitude']
       writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames)
       reader = csv.DictReader(csvinput)
       for row in reader:
            #here you have to replace the dict item by your csv column names
            query = ','.join(str(x) for x in (row['Country'],row['Province'],row['Area']))
            errorquery = ','.join(str(x) for x in (row['Country'],row['Province'],row['Province']))
            try:
                # actually don't even need to pass lookups
                # because you declared it globally
                address, latitude, longitude = lookup_location(query, lookups)
            except: # should catch whatever error is generated by the geocoder
                address, latitude, longitude = lookup_location(errorquery, lookups)

            output_row = {}
            output_row['Name'] = row['Name']
            output_row['Status'] = row['Status']
            output_row['Sex'] = row['Sex']
            output_row['Country'] = row['Country']
            output_row['Province'] = row['Province']
            output_row['Area'] = row['Area']
            output_row['Date_of_Death'] = row['Date_of_Death']
            output_row['Cause_of_Death'] = row['Cause_of_Death']

            output_row['Latitude'] = latitude
            output_row['Longitude'] = longitude
            writer.writerow(output_row)

我稍微更改了代码,使其对我有用。感谢您的想法和帮助。

import csv
from geopy.geocoders import GoogleV3

geolocator = GoogleV3() #here some parameters are needed
lookups = {}


with open('151213_Martyrs_filtered_civilian_2015_with_country.csv', 'rb') as csvinput:
    with open('151213_output_martyrs_final.csv', 'w') as csvoutput:
        output_fieldnames = ['Name','Status','Sex','Country','Province','Area','Date_of_Death','Cause_of_Death','Latitude','Longitude']
        writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames)
        reader = csv.DictReader(csvinput)
        for row in reader:
            if row['Province'].split(' ', 1) == 'Damascus':
                rowProv = "Damascus"
            else:
                rowProv = row['Province']
            #here you have to replace the dict item by your csv column names
            query = ','.join(str(x) for x in (rowProv, row['Area']))

            try:
                address, (latitude, longitude) = lookups[query]
            except KeyError: # new lookup
                try:
                    query = ','.join(str(x) for x in (rowProv, rowProv))
                    address, (latitude, longitude) = lookups[query]
                    print('lookup')
                except KeyError: # search
                    try:
                        query = ','.join(str(x) for x in (rowProv, row['Area']))
                        address, (latitude, longitude) = geolocator.geocode(query)
                    except: # error
                        query = ','.join(str(x) for x in (rowProv, rowProv))
                        address, (latitude, longitude) = geolocator.geocode(query)
                    lookups[query] = address, (latitude, longitude)
                    print(query)

            #here is the writing section
            output_row = {}
            output_row['Name'] = row['Name']
            output_row['Status'] = row['Status']
            output_row['Sex'] = row['Sex']
            output_row['Country'] = row['Country']
            output_row['Province'] = row['Province']
            output_row['Area'] = row['Area']
            output_row['Date_of_Death'] = row['Date_of_Death']
            output_row['Cause_of_Death'] = row['Cause_of_Death']
            output_row['Latitude'] = latitude
            output_row['Longitude'] = longitude
            writer.writerow(output_row)