github/python/ show me the code 25题(二)
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}
请将上述内容写到 student.xls 文件中,如下图所示:
#coding:utf-8
#!/usr/bin/python
import sys,os
import json
import xlwt
#f = open('students.txt')
with open (os.getcwd()+'\students.txt') as f:
dict = json.loads(f.read().decode('gbk')) #json对应到python就是dict类型
xls = xlwt.Workbook() #创建一个xls对象
sheet = xls.add_sheet('student')
for i in range(len(dict.keys())):
row = i
col = 0
sheet.write(row,col,dict.keys()[i])
for j in dict[dict.keys()[i]]:
col += 1
sheet.write(row,col,j)
xls.save(os.getcwd()+'\student.xls')
xls文件的读写,涉及到了xlwt,xlrd等库。这个题目把字典信息写入xls文件。
利用json将信息结构化的读入,再用xlrt写进空的xls文件里
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
这个题和上面没什么不一样,代码就不放了。
第 0016 题: 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:
[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]
注意这次的文本,实际上是个元素为序列的序列
#coding:utf-8
import os,json
import xlwt with open(os.getcwd()+'/numbers.txt') as f:
list = json.loads(f.read().decode('gbk'))
xls = xlwt.Workbook()
sheet = xls.add_sheet('city') #只有sheet才可以写
for i in range(len(list)):
col = 0
for j in list[i]:
sheet.write(i,col,j)
col += 1 xls.save(os.getcwd()+'/number.xls')
json.loads是个蛮神奇的函数对吧,它可以识别出你的文本究竟是个什么样的结构,是个字典,还是一个序列?
上面的两道题里,文本都是字典信息,所以在代码中为了便于理解,将变量名就起为了dict,同时这个dict也的确是个dict,在写入的时候,就是按dict的用法,将信息写入xls。
这道题的道理也是一样,重点还是理解dict和list的结构与用法。
第 0017 题: 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如
下所示:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<students>
<!--
学生信息表
"id" : [名字, 数学, 语文, 英文]
-->
{
"1" : ["张三", 150, 120, 100],
"2" : ["李四", 90, 99, 95],
"3" : ["王五", 60, 66, 68]
}
</students>
</root>
哇塞,直接就从dict,list跳到了xml?????
git页面里已经给了相关资料的链接:http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html
#coding:utf-8
import xlrd
import os,json
from lxml import etree
xls = xlrd.open_workbook(os.getcwd()+'\student.xls')
sheet = xls.sheet_by_name("student")
Dict = {}
for row in range(sheet.nrows):
Attr = sheet.row_values(row) ##此处用法
Dict[Attr[0]] = Attr[1:] #形成key-value对 #如何操作xml
root = etree.Element('root')
child1 = etree.SubElement(root,"student")
comm = etree.Comment(u'学生信息表 "id":[名字,数学,名字,英文]')
child1.append(comm)
child1.text = unicode(json.dumps(Dict).decode('gbk')) print child1.text.decode('gbk')
tree = etree.ElementTree(root)
tree.write(os.getcwd()+'\student.xml')
这里说句实在话,python里读写主要就是看库与接口,另一个就是要看相关文件的结构。
我对xml基本是不了解,所以也就不展开说明了。
第 0018 题: 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示:
<?xmlversion="1.0" encoding="UTF-8"?>
<root>
<citys>
<!--
城市信息
-->
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
</citys>
</root>
#coding:utf-8
import xlrd
import os,json
from lxml import etree
xls = xlrd.open_workbook(os.getcwd()+'\city.xls')
sheet = xls.sheet_by_name("city")
Dict = {}
for row in range(sheet.nrows):
Attr = sheet.row_values(row) ##此处用法
Dict[Attr[0]] = Attr[1:] #形成key-value对 #如何操作xml
root = etree.Element('root')
child1 = etree.SubElement(root,"numbers")
comm = etree.Comment(u'城市信息')
child1.append(comm)
child1.text = unicode(json.dumps(Dict).decode('gbk')) print child1.text.decode('gbk')
tree = etree.ElementTree(root)
tree.write(os.getcwd()+'\city.xml')
第 0019 题: 将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,如下
所示:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<numbers>
<!--
数字信息
-->
[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]
</numbers>
</root>
#coding:utf-8
import xlrd
import os,json
from lxml import etree
xls = xlrd.open_workbook(os.getcwd()+'/number.xls')
sheet = xls.sheet_by_name("city")
List = {}
for row in range(sheet.nrows):
Attr = sheet.row_values(row) ##此处用法
List[row] = Attr #形成key-value对 #如何操作xml
root = etree.Element('root')
child1 = etree.SubElement(root,"numbers")
comm = etree.Comment(u'数字信息')
child1.append(comm)
child1.text = unicode(json.dumps(List).decode('gbk')) print child1.text.decode('gbk')
tree = etree.ElementTree(root)
tree.write(os.getcwd()+'/number.xml')
github/python/ show me the code 25题(二)的更多相关文章
- github/python/ show me the code 25题(一)
先上网址 https://github.com/Show-Me-the-Code/show-me-the-code 初学python拿来练手,记住一些常用的库和函数 第 0000 题:将你的 QQ 头 ...
- 剑指offer 面试25题
面试25题:题目:合并两个排序的链表 题:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 解题思路:递归,并需注意对空链表单独处理. 解题代码: # -* ...
- 通过欧拉计划学习Rust编程(第22~25题)
最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,所以先补一下Rust的基础知识.学习了一段时间,发现Rust的学习曲线非常陡峭,不过仍有快速入门的办法. 学习任何一项技能最怕没有 ...
- 各位大佬Python的第一部分道基础题已经整理好了,希望大家面试的时候能用的上。
Python的第一部分道基础题,希望大家面试的时候能用的上. 1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之一. 2.通过什么途 ...
- FaceRank-项目上了 GitHub Python Trending
FaceRank-人脸打分基于 TensorFlow 的 CNN 模型 GitHub Python Trending 第一次上 trending ,虽然只是分类榜. https://github.co ...
- [转]Python in Visual Studio Code
本文转自:https://code.visualstudio.com/docs/languages/python Working with Python in Visual Studio Code, ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案
国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...
- 学习《零基础入门学习Python》电子书PDF+笔记+课后题及答案
初学python入门建议学习<零基础入门学习Python>.适合新手入门,很简单很易懂.前一半将语法,后一半讲了实际的应用. Python3入门必备,小甲鱼手把手教授Python,包含电子 ...
随机推荐
- Egret及Node.js的安装部署
最近在学Html5游戏开发,我选择的是国内的一个游戏开发框架egret.因为涉及到node.js这个近年来新兴起来的技术.借此机会把这方面知识学习一下. node.js以及egret的操作类似于Lin ...
- ASP.NET MVC 与Form表单交互
一,Form包含文件类(单选文件) <form id="ImgForm" method="POST" enctype="multipart/fo ...
- 混淆篇之原生DOM操作方法小结
1.0 DOM结构 1.1先来看结构图: 父节点 兄弟节点 当前节点 属性节点 子节点 兄弟节点一般任意一个节 ...
- EC读书笔记系列之16:条款35、36、37、38、39、40
条款35 考虑virtual函数以外的其他选择 记住: ★virtual函数的替代方案包括NVI手法及Strategy模式的多种形式.NVI手法自身是一个特殊形式的Template Method模式 ...
- sql防注入代码
function defend_sql($string, $force = 1) { $preg = "select|insert|and|or|update|delete|\'|\/\*| ...
- C#学习日志 day10 -------------- problem statement
Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...
- verilog中=和<=的区别
一般情况下使用<=,组合逻辑使用=赋值,时序逻辑使用<=赋值: 举个例子:初始化m=1,n=2,p=3:分别执行以下语句 1.begin m=n:n=p:p=m: end 2.begin ...
- 安装solaris_11.2与windows双系统(VM10模拟实现)(一)
感慨:这周刚接触solaris,装solaris很蛋疼,一个字:慢! 在上面安装软件包依然很慢,无线网也很不稳定. 在上面搭建环境更蛋疼,一个字:惨! 什么环境之类的废话就不多说了,一般的电脑都可以. ...
- JQ中$(document.ready())
js中window.onload与jquery中$(document.ready())的区别 <html> <head> <script type='text/javas ...
- 转: ES6异步编程: co函数库的含义与用法
转: ES6异步编程: co函数库的含义与用法 co 函数库是著名程序员 TJ Holowaychuk 于2013年6月发布的一个小工具,用于 Generator 函数的自动执行. 比如,有一个 Ge ...