您的位置:首页 > 编程语言 > Python开发

练习014-015

2016-05-21 12:24 288 查看
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:

{

“1”:[“张三”,150,120,100],

“2”:[“李四”,90,99,95],

“3”:[“王五”,60,66,68]

}

请将上述内容写到 student.xls 文件中,如下图所示:

student.xls



第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:

{

“1” : “上海”,

“2” : “北京”,

“3” : “成都”

}

请将上述内容写到 city.xls 文件中,如下图所示:

city.xls



#coding:utf-8
import xlsxwriter
def read():
list = []
dict = {}
with open('12.txt' ,'r') as file:
while True:
x = file.readline()
if x:
if len(x)>2:
x = x.strip()
if x.endswith(','):
x = x[:-1]
list.append(x.decode('gbk'))
else:
list.append(x.decode('gbk'))
else:
break

for i in list:
dict[i.split(':')[0].strip()]=i.split(":")[1].strip()
return dict
def write():

dict = read()
list = ['A','B','C','D','E','F']
workbook = xlsxwriter.Workbook('11.xlsx')
worksheet= workbook.add_worksheet('11')
for i in dict:
x = 0
aa= list[x]+i[1:-1]
if i.endswith('"'):
worksheet.write( aa ,i[1:-1])
x=1
for j in dict[i][1:-1].split(','):
if j.endswith('"'):
j = j[1:-1]
aa = list[x]+i[1:-1]
x=x+1
worksheet.write(aa,j.encode('gbk').decode('gbk') )
workbook.close()
print "已经写完了"

if __name__ == "__main__":
write()


两个题可以用同一个代码解决

代码可能写的有点繁琐,请高手不要吐槽啊!!!





( 写于2016年5月21日,http://blog.csdn.net/bzd_111

更新于2016年5月25日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xlsxwrite python