excel表格读取、复制+百度地图接口调用处理地理信息+写入保存

import requests
import re
import json
import xlrd,xlwt
from xlutils.copy import copy

'''获取地址坐标'''


def get_get_location_m(name):
    url = "https://restapi.amap.com/v3/place/text?s=rsv3" \
          "&children=&key=8325164e247e15eea68b59e89200988b&page=1" \
          "&offset=10&city=510100&language=zh_cn" \
          "&callback=jsonp_755735_" \
          "&platform=JS&logversion=2.0" \
          "&sdkversion=1.3" \
          "&appname=https%3A%2F%2Flbs.amap.com%2Fconsole%2Fshow%2Fpicker" \
          "&csid=F028E84F-6601-43AE-88A8-13425E3DE7C7" \
          "&keywords={}".format(name)


    res_text = requests.get(url).text

    if re.findall('"info":"OK"', res_text):
        if json.loads(res_text.replace(re.findall("jsonp_\d+_\(", res_text)[0], "")[0:-1])["pois"] == []:
            return None
        else:
            res_data = json.loads(res_text.replace(re.findall("jsonp_\d+_\(", res_text)[0], "")[0:-1])["pois"][0]
            item = {
     }
            item["name"] = res_data["name"]
            item["type"] = res_data["type"]
            item["location"] = res_data["location"]
            item["pname"] = res_data["pname"]
            item["cityname"] = res_data["cityname"]
            item["adname"] = res_data["adname"]
            return item
    else:
        return None

def changeGender(gender):
    file = xlrd.open_workbook(r'C:\Users\Cheryl.Chen\Desktop\CN_Clients_Survey_0806 - Copy.xlsx')

if __name__ == "__main__":
    file = xlrd.open_workbook(r'C:\Users\Cheryl.Chen\Desktop\CN_Clients_Survey_0806 - Copy.xlsx')
    sheet1 = file.sheet_by_name('CN Clients Survey')
    filecopy = copy(file)
    sheet = filecopy.get_sheet(0)
    for row in range(len(sheet._Worksheet__rows)):
        gender = sheet1.cell_value(row, 1)
        if gender == '先生' or gender == 'Mr.' or gender == 'Mr' or gender=='男性':
            sheet.write(row, 1, 'Male')
        elif gender == '女士' or gender == 'Mrs.' or gender=='Mrs' or gender=='Ms.' or gender=='Ms' or gender=='Miss.' or gender=='Miss':
            sheet.write(row, 1, 'Female')
        else:
            sheet.write(row, 1, 'NaN')
        if sheet1.cell_value(row, 2) != 'Client Age':
            age = int(sheet1.cell_value(row, 2))
            if 0 < age < 18:
                sheet.write(row, 2, '<18')
            elif 18 <= age <= 25:
                sheet.write(row, 2, '18-25')
            elif 26 <= age < 36:
                sheet.write(row, 2, '26-35')
            elif 36 <= age < 46:
                sheet.write(row, 2, '36-45')
            elif 46 <= age <= 60:
                sheet.write(row, 2, '46-60')
            elif 60 < age:
                sheet.write(row, 2, '>60')

        location = sheet1.cell_value(row, 4) + sheet1.cell_value(row, 5) + sheet1.cell_value(row, 6)
        # print(location)
        getLocationResult = get_get_location_m(location)
        if getLocationResult == None:
            sheet.write(row, 4, 'NaN')
            sheet.write(row, 5, 'NaN')
        else:
            sheet.write(row, 4, getLocationResult['pname'])
            sheet.write(row, 5, getLocationResult['cityname'])
    filecopy.save(u'xxx.xls')




    # print(get_get_location_m("ANHUI  安庆市    枞阳县项铺镇边山村新屋组19号"))
    # {'name': '边山村', 'type': '地名地址信息;普通地名;村庄级地名', 'location': '117.306637,30.881471', 'pname': '安徽省', 'cityname': '铜陵市',
     # 'adname': '枞阳县'}

你可能感兴趣的:(笔记)