(중괄호 포함) 내용은 다음과 같습니다 일반 텍스트 파일 student.txt 학생 정보 :
{ "1": "조 스미스"150,120,100, "2": "홍길동"90,99,95, "3": "왕 우"60,66,68]}
아래 그림과 같이, 위의 student.xls 파일을 작성하십시오 :
-
#!/usr/bin/python
-
# -*- coding: utf-8 -*-
-
from collections import OrderedDict
-
-
import xlwt,json
-
-
with open('Python3v\python3v\test\student.txt','r', encoding="utf-8") as f:
-
data = json.load(f, object_pairs_hook=OrderedDict)
-
workbook = xlwt.Workbook()
-
sheet1 = workbook.add_sheet('student', cell_overwrite_ok=True)
-
for index, (key, values) in enumerate(data.items()):
-
sheet1.write(index, 0, key)
-
for i, value in enumerate(values):
-
sheet1.write(index, i+1, value)
-
workbook.save('Python3v\python3v\test\student.xls')