Search code examples
pythongeopy

error: 'str' object does not support item assignment in python


I am trying to geocode address using geopy by using some code downloaded from github. but the error the code is that the str obj doesn't support item assignment in geocode_addresses address_dict["error"] = ""? why do I get that error. below is the code.

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

dataset example

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

Solution

  • Here it looks like address/df.ADDRESS in the snippet below:

        address = df.ADDRESS
        geocoded = geocode_addresses(address)
    

    address is not a list of dicts like you're expecting it to be:

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

    after testing your code, here's what address_dicts is:

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

    which is basically a (pandas) list of strings:

    (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;'
    

    so your issue is related to how you're parsing the CSV file.


    And tbh, I don't really understand what you're trying to do here:

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

    so you're opening the file, then you iterate over the file, and for each line you're parsing the file as a 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)
    

    and then you need to rethink the way you're parsing the values. Don't hesitate to call pdb or add printouts.