Python folium, geojson, csv questions. UnicodeDecodeError: 'cp949' codec can't decode

Asked 2 years ago, Updated 2 years ago, 101 views

Below is the code that I made.

 #-*- coding: utf-8 -*-
import folium
from folium import plugins
import pandas as pd
import json
import geojson


country_data='row_deal_s.csv'
df = pd.read_csv('row_deal_s.csv')
df.head()
geo_data = '32.geojson'
keys=[k['id'] for k in json.load(open('32.geojson'))['features']]
missing_keys=set(keys)-set(plot_data[country_data])
dicts=[]
for k in missing_keys:
   row={}
   dicts.append({country_data: k, 'Value': 0})
dicts
mapdata = country_data
mapdata = mapdata.append(dicts, ignore_index=True)

center = [36.48, 127.29]
m = folium.Map(location=center, zoom_start=10)
folium.Choropleth(
    geo_data=geo_data,
    data=df,
    columns=('Dongeup-myeon', 'average transaction amount (KRW 10,000)'),
    key_on='feature.dong',
    fill_color='BuPu',
    legend_name='Sold price',
).add_to(m)
m.save('/Users/pc/Desktop/hoon/map1.html')

Below is an error statement

Traceback (most recent call last):
  File "C:\Users\pc\Desktop\hoon\raw.py", line 13, in <module>
    keys=[k['id'] for k in json.load(open('32.geojson'))['features']]
  File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 40: illegal multibyte sequence

Both geojson and csv files have utf8 encoding completed. However,

I keep getting encoding errors, so I'd appreciate it if you could help me (utf8, cp949, 'r', 'rt' I tried everything)

I'd appreciate it if you could check if there's any problem with the code Thank you

python unicodedecodeerror

2022-09-20 17:57

1 Answers

Looking at the error message, a problem occurred while trying to read 32.geojson with 'cp949' encoding. 32.geojson If this is encoded as 'utf-8', please read it below before switching to json.

with open('32.geojson', 'r', encoding='utf-8') as f:
  txt = f.read()
print(txt)

If the above code is executed without a problem, you can change the open('32.geojson') part to open('32.geojson','r', encoding='utf-8') in the above questioner's code.

If an error occurs, you should look at the error message and look carefully at the '32.geojson' file.


2022-09-20 17:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.