파이썬) json 파일 생성할 때 float형을 string으로 어떻게 변환하나요?
조회수 1150회
엑셀 파일을 json 파일로 변환하려고 코드를 짜고 있습니다. 그러던 중 string으로 들어가야 할 부분이 float 형으로 들어가서 애를 먹고 있는데요, float형을 어떻게 string으로 바꿔야하는지 모르겠습니다.
아래는 진행 중인 코드입니다.
import xlrd
from collections import OrderedDict
import json
excel_path = 'data.xlsx'
wb = xlrd.open_workbook(excel_path)
sh = wb.sheet_by_index(0)
data_list = []
for rownum in range(1, sh.nrows):
data = OrderedDict()
row_values = sh.row_values(rownum)
data['col1'] = int(row_values[0])
if type(row_values[1]) == float:
data['col2'] = chr(row_values[1]) # 이 부분입니다!
else:
data['col2'] = row_values[1]
data['col3'] = row_values[2]
data['col4'] = row_values[3]
data['col5'] = int(row_values[4])
data['col6'] = row_values[5]
data['col7'] = row_values[6]
data['col8'] = row_values[7]
data['col9'] = row_values[8].split(', ')
data_list.append(data)
j = json.dumps(data_list, ensure_ascii=False) #한글 정상표시를 위해 필수
with open('data.json', 'w', -1, "utf-8") as f:
f.write(j)
print(j)
2 답변
-
str(1.2345)
그런데 애초에 굳이 string으로 또 변환해야하는 이유가 있나요?
그냥 하면 바로 되지 않나요?? json dump 로..
참고.
-
댓글 입력