补充知识

__doc__:.py文件的注释

__file__:当前文件路径

__package__:当前文件所在包,用(.)分隔

__cached__:缓存

__name__:如果是主文件,__name__=="__main__否则,等于模块名

#差看当前路径
print(__file__)


#输出结果
D:/python_code/buchongmokuai.py
import os
#返回上级目录
print(os.path.dirname(__file__)



#输出结果
D:/python_code
#将字符串转换成字典
s = '{"desc":"invilad-citykey","status":123}'
result = json.loads(s)
print(result)


#输出结果
{'status': 123, 'desc': 'invilad-citykey'}
#将类似列表的字符串转换成列表
w = '[11,22,33,44,55,66]'
result = json.loads(w)
print(result,type(result))


#输出结果
[11, 22, 33, 44, 55, 66] <class 'list'>
#将列表转换成字符串
user_list = ["alex","eric","tomy"]
s = json.dumps(user_list)
print(s,type(s))



#输出结果
["alex", "eric", "tomy"] <class 'str'>
#将元祖转换成列表
w = (11,22,33,44,55,66,"alex")
s = json.dumps(w)
print(s)


#输出结果
[11, 22, 33, 44, 55, 66, "alex"]
import requests

srsponse = requests.get("http://www.weather.com.cn/adat/sk/101010500.html")             #请求获取域名属性
response.encoding = "utf-8"        #转换成 "utf-8"编码类型并赋给响应(response)
result = responst.text                 #文档接收并赋给结果(result)
print(result)


#输出结果
{"weatherinfo":{"city":"怀柔","cityid":"101010500","temp":"9","WD":"南风","WS":"1级","SD":"29%","WSE":"1","time":"10:25","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1007"}}



requests:请求模块
get:获取属性
response:响应
result:结果
#解析XML格式内容
from xml .etree import ElementTree as ET
r = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=K234&UserID=')   #使用第三方模块requests发送HTTP请求,或者XML格式
result = r.text         #文档接收,r,xml解析xml格式后的内容
root = ET.XML(result)   
for node in root.iter("TrainDetailInfo"):     循环root迭代(inte)的标签
    print(node.find("TrainStation").text,node.find("StartTime").text)
#找到(TrainStation)标签下的(StartTime)内容
#创建一个以xml结尾的文件
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year age="19">2040</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year age="19">2043</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year age="19">2043</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>
########################
get:获取属性                                   
set:设置属性
iter:迭代
tag:根节点
attrib:标签属性 #########################

 

########################################

from xml.etree import ElementTree as ET
#打开并解析文件内容
tree = ET.parse("first.xml")      
root = tree.getroot()
for node in root.iter("year"):
    new_year = int(node.text) + 1 #获取节点的值加一,并重新赋值
    node.text = str(new_year)  #在这个year标签插入
    node.set("name","wyc")     #添加节点
    node.set("sge","19")          #添加节点
    del node.attrib["name"]     #删除改变节点的属性
tree.write("first.xml")          #写入(first.xml)

 

posted @ 2016-05-24 23:52  WrYcF  阅读(37)  评论(0编辑  收藏  举报
Live2D