error: 'str' object does not support item assignment in python
error: 'str' object does not support item assignment in python
我正在尝试使用从 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";
下面的代码片段看起来像 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 或添加打印输出。
我正在尝试使用从 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";
下面的代码片段看起来像 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 或添加打印输出。