github/python/ show me the code 25題(二)

第 0014 題: 純文本文件 student.txt爲學生信息, 裏面的內容(包括花括號)以下所示:html

{
    "1":["張三",150,120,100],
    "2":["李四",90,99,95],
    "3":["王五",60,66,68]
}

請將上述內容寫到 student.xls 文件中,以下圖所示:python

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文件。git

利用json將信息結構化的讀入,再用xlrt寫進空的xls文件裏github

 

第 0015 題: 純文本文件 city.txt爲城市信息, 裏面的內容(包括花括號)以下所示:json


這個題和上面沒什麼不同,代碼就不放了。{ "1" : "上海", "2" : "北京", "3" : "成都" }

第 0016 題: 純文本文件 numbers.txt, 裏面的內容(包括方括號)以下所示:app

[
    [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。spa

這道題的道理也是同樣,重點仍是理解dict和list的結構與用法。code

 

第 0017 題: 將 第 0014 題中的 student.xls 文件中的內容寫到 student.xml 文件中,如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')
相關文章
相關標籤/搜索