Python-常用库

import time
print time.strftime('%y-%m-%d %H:%M:%S',time.localtime())
19-04-21 13:03:08
# coding:utf-8
import os
print u'当前文件的目录',os.path.dirname(__file__)
print u'当前文件的上一级目录',os.path.dirname(os.path.dirname(__file__))
base_dir = os.path.dirname(os.path.dirname(__file__))
f = open(os.path.join(base_dir,'Days/login'),'r')
print f.read()
python执行路径搜索:
程序根目录
环境变量
标准库目录
第三方库的目录

json字典的序列化与反序列化
序列化:把python的数据类型转化为str的类型过程 dumps
反序列化:str的类型转化为Python的数据结构 loads
import json
import requests
r = requests.post(url = 'https://ecapi.parkingwang.com/v5/login',
headers ={ 'Parkingwang-Client-Source':'ParkingWangAPIClientWeb',
'Content-Type': 'application/json;charset=UTF-8'},
data = json.dumps({"source":"common","password":""}))
print r.status_code
print r.text
print json.loads(str(r.text))['msg']

import json
import requests
r = requests.get(url = 'http://www.weather.com.cn/data/sk/101190408.html')
'''文件的序列化与反序列化'''

对文件的反序列化就是读取文件的内容
文件反序列化后是unicode
进行编码,把unicode转为str类型
反序列化,把str转为字典类型

dict1 = json.loads((json.load(open('weather.json','r'))).encode('utf-8'))
print dict1['weatherinfo']['city']

# coding:utf-8


import json
import requests
r = requests.get(url = 'http://www.weather.com.cn/data/sk/101190408.html')
# result = r.content.decode('utf-8')
result = r.content
print result
r = json.dump(result,open('weather.json','w'))
w = json.load(open('weather.json','r'))
result1 = json.loads(w)
print result1["weatherinfo"]["city"]
 

 
 






 

原文地址:https://www.cnblogs.com/hyzhang/p/10744895.html