forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (39 loc) · 1.01 KB
/
main.py
File metadata and controls
54 lines (39 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding:utf-8 -*-
'''
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
请将上述内容写到 city.xls 文件中,如下图所示:
@Author monkey
@Date 2017-8-31
'''
import json
import xlwt
def getCity():
with open('city.txt', 'r', encoding='UTF-8') as file:
text = ''
for line in file:
text = text + line
city_json = json.loads(text, encoding = 'UTF-8')
print(city_json)
writeInXLS(city_json)
def writeInXLS(dict):
fileName = 'city.xls'
# 创建 文件
file = xlwt.Workbook()
# 创建 表
sheet = file.add_sheet('city', cell_overwrite_ok=True)
row = 0
col = 0
for k, v in sorted(dict.items(), key=lambda d:d[0]):
sheet.write(row, col, k)
col += 1
sheet.write(row, col, v)
row += 1
col = 0
file.save(fileName)
if __name__ == '__main__':
getCity()