我有一个带有自由格式地址的数据框,可以使用Google Maps Geocoder API对其进行规范化。结果以JSON返回,写在列中并保存到csv。
但是,当我稍后再上传此csv时,反斜杠出现在带有JSON的列中,因为该列是从csv中以字符串形式读取的,所以单引号出现在开头和结尾,而json内的单引号用反斜杠。
我明白了
json_address
'{\'status\': \'OK\'}'
我需要
json_address
{'status': 'OK'}
标记为重复的问题中的解决方案对我不起作用,因为我的csv用单引号读取,并且出现错误Expecting property name enclosed in double quotes
除此之外,我不想标准化JSON并将其放在单独的列中,我希望将输出作为上面指定的字典
我试图用双引号替换单引号
df['json_double'] = df['json_address'].apply(lambda x: x.replace("\'", "\""))
它起作用了,所以我得到了
''{\''status\'': \'OK\''}''
但是,当我尝试df['json'] = df['json_double'].apply(json.loads)
时遇到错误Expecting ',' delimiter: line 1 column 609 (char 608)
所以我的问题是
PS整个json字符串为
'{\'results\': [{\'address_components\': [{\'long_name\': \'4\', \'short_name\': \'4\', \'types\': [\'street_number\']}, {\'long_name\': \'Ulitsa Marshala Rokossovskogo\', \'short_name\': \'Ulitsa Marshala Rokossovskogo\', \'types\': [\'route\']}, {\'long_name\': \'Nizhnij Novgorod\', \'short_name\': \'Nizhnij Novgorod\', \'types\': [\'locality\', \'political\']}, {\'long_name\': \'Sovetskiy\', \'short_name\': \'Sovetskiy\', \'types\': [\'administrative_area_level_3\', \'political\']}, {\'long_name\': \'Gorod Nizhniy Novgorod\', \'short_name\': \'Gorod Nizhniy Novgorod\', \'types\': [\'administrative_area_level_2\', \'political\']}, {\'long_name\': "Nizhegorodskaya oblast\'", \'short_name\': "Nizhegorodskaya oblast\'", \'types\': [\'administrative_area_level_1\', \'political\']}, {\'long_name\': \'Russia\', \'short_name\': \'RU\', \'types\': [\'country\', \'political\']}, {\'long_name\': \'603162\', \'short_name\': \'603162\', \'types\': [\'postal_code\']}], \'formatted_address\': "Ulitsa Marshala Rokossovskogo, 4, Nizhnij Novgorod, Nizhegorodskaya oblast\', Russia, 603162", \'geometry\': {\'location\': {\'lat\': 56.28278, \'lng\': 44.0456111}, \'location_type\': \'ROOFTOP\', \'viewport\': {\'northeast\': {\'lat\': 56.2841289802915, \'lng\': 44.0469600802915}, \'southwest\': {\'lat\': 56.2814310197085, \'lng\': 44.0442621197085}}}, \'place_id\': \'ChIJMeknJyXVUUERI9A8HCXBznI\', \'plus_code\': {\'compound_code\': \'72MW+46 Nizhny Novgorod, Nizhny Novgorod Oblast, Russia\', \'global_code\': \'9H8672MW+46\'}, \'types\': [\'street_address\']}], \'status\': \'OK\'}'
对其应用json.loads
会导致Expecting ',' delimiter: line 1 column 609 (char 608)