-2

我正在尝试通过使用从 github 下载的一些代码来使用 geopy 对地址进行地理编码。但是代码的错误是str obj不支持项目分配geocode_addresses address_dict["error"] = ""?为什么我会收到这个错误。下面是代码。

if __name__ == '__main__':
    csv_file = 'nr-fixedNew.csv'
    with open(csv_file, 'rb') as csvfile:
        for row in csvfile.readlines():
            df = pd.read_csv(csv_file)
            address = df.ADDRESS
            geocoded = geocode_addresses(address)
            write_csv(output_file, geocoded)


    def geocode_addresses(address_dicts):
        geocoder = geocoders.GoogleV3()
        for address_dict in address_dicts:
            address_dict["error"] = ""
            try:
                time.sleep(1)
                address, (lat, lon) = geocoder.geocode(address_dict["fulladdress"])
                address_dict["fulladdress"] = address
                address_dict["latitude"] = lat
                address_dict["longitude"] = lon
            except ValueError as e:
                address_dict["error"] = e
        return address_dicts

数据集示例

SEX,PROGRAMME,ADDRESS
M,2,"J6855, JALAN LMBAH KESANG 1/1-3,77378 MERLIMAU, MELAKA";
4

1 回答 1

0

这里看起来像address/df.ADDRESS在下面的代码段中:

    address = df.ADDRESS
    geocoded = geocode_addresses(address)

address不是您期望的字典列表:

for address_dict in address_dicts:
    address_dict["error"] = ""

测试您的代码后,这address_dicts是:

(<class 'pandas.core.series.Series'>

这基本上是一个 ( pandas) 字符串列表:

(Pdb) address_dicts
0    J6855, JALAN LMBAH KESANG 1/1-3,77378 MERLIMAU...
Name: ADDRESS, dtype: object
(Pdb) address_dicts[0]
'J6855, JALAN LMBAH KESANG 1/1-3,77378 MERLIMAU, MELAKA;'

所以您的问题与您解析 CSV 文件的方式有关。


而且,我真的不明白你在这里想要做什么:

with open(csv_file, 'rb') as csvfile:
    for row in csvfile.readlines():
        df = pd.read_csv(csv_file)

所以你正在打开文件,然后你遍历文件,并且对于每一行你将文件解析为 csv ?!


if __name__ == '__main__':
    csv_file = 'nr-fixedNew.csv'
    with open(csv_file, 'rb') as csvfile:
            df = pd.read_csv(csv_file).to_dict() ### here you convert to dict
            address = df['ADDRESS']
            geocoded = geocode_addresses(address)
            write_csv(output_file, geocoded)

然后您需要重新考虑解析值的方式。不要犹豫调用 pdb 或添加打印输出。

于 2016-03-10T16:59:55.103 回答