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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值