美文网首页
Python 随手记

Python 随手记

作者: 松上有婵 | 来源:发表于2019-08-07 09:27 被阅读0次
1. 拼接路径时,python 用
os.path.join("a","b","c","d")

会根据操作系统的不同拼接出正确的路径。

2. 多行字符串格式化。

例子:

raws=""" 
The first string is %s
The second string is %s
The last string is %s
"""%('1','2','3')

记得格式化"""%('1','2','3')这里不要有空格

3. requests获取网页响应时间用法:单位是微秒
import requests
r = requests.get("http://www.baidu.com")
r.elapsed.microseconds
4. pymongo 显示报错信息cursor id is not valid at server

db.collection.find() 返回的是一个cursor,而不是所有数据,
比较好的解决办法是:控制batch_size的数量,让其在10分能跑完,避免cursor超时。

for doc in db.collection.find().batch_size(50):
5. python 快速生成包含同一元素的列表方法:
port = 22
portList = [port] * 10
#生成结果[22,22,22,22,22,22,22,22,22,22]
6. requests的使用
#忽略证书错误
requests.get('https://baidu.com', verify=False)
#使用代理获取百度首页
import requests
 headers = {
          "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
## 定义代理的字典
proxies = {
          # "http":"http://114.18.52.4:6666"
          # "https":"https://114.19.30.126:8888"
          # "http":"http://username:password@ip:端口号" #带账号密码的代理
          #"https": "https://username:password@ip:端口号"
          "http": "http://120.28.18.93:8001"
}
 ## 使用代理给服务器发送请求
response = requests.get("https://www.baidu.com", proxies=proxies, headers=headers)
## 获取状态
print(response.status_code)
print(response.content.decode())
7. pymongo 把结果json化
#数据库连接文件dbc.py
import pymongo, sys, traceback

MONGODB_CONFIG = {
    'host': '192.168.1.1',
    'port': 27017,
    'db_name': 'test',
    'username': 'test',
    'password': 'test'
}


class MongoConn(object):
    def __init__(self):
        try:
            self.conn = pymongo.MongoClient(MONGODB_CONFIG['host'], MONGODB_CONFIG['port'])
            self.db = self.conn[MONGODB_CONFIG['db_name']]
            self.username = MONGODB_CONFIG['username']
            self.password = MONGODB_CONFIG['password']
            if self.username and self.password:
                self.connect = self.db.authenticate(self.username, self.password)
            else:
                self.connect = True
        except Exception:
            print(traceback.format_exc())
            print("Connect db Fail!!")
            sys.exit(1)
#结果按json格式输出
from dbc import MongoConn
from bson.objectid import ObjectId
from bson import json_util as jsonb
md = MongoConn()
result = md.db['sshDb'].find({"tryPassWord": {"$nin": [password]}}, {'ip': 1, "_id": 0}).batch_size(10)
res = json.loads(jsonb.dumps(result))
print(res)
8. 打开大文件如何减少内存占用,错误提示MemoryError。
#文本文件用直接迭代文件
with open('password.txt', 'r', 'utf-8', buffering=True)as lines:
    for line in lines:
        print(line)
#或者
with open('password.txt', 'rb', 'utf-8', buffering=True) as f:
    while True:
        line = f.readline()
        if line:    
            print(line)
        else:
            break

#二进制文件
with open('password.txt', 'rb') as f:
    while True:
        buf = f.read(1024)
        if buf:    
            sha1Obj.update(buf)
        else:
            break

小端转大端(大端为适合人类阅读顺序)

#适用于python2
"6fe28c".decode('hex')[::-1].encode('hex_codec')
# output: '8ce26f'

jupyter notebook打开命令

#cmd 下运行
jupyter notebook

conda常用命令

#创建环境,py27是环境别名,2.7是python的版本
conda create -n py27 python=2.7
#查看所有安装的环境
conda env list
#激活环境
conda activate py27
在当前目录安装lxml依赖包
pip install lxml -t .

相关文章

  • 随拍,手记。

  • python列表排序、字典排序、列表中字典排序

    手记 -- encoding=utf-8 -- python3代码 import operator一. 按字典值排...

  • 2018-10-16

    python学习笔记 标签:用廖雪峰老师的python文档学习,顺手记一点 两种除法 (1)一种除法是/: /除法...

  • python手记:入门

    python的下载地址:https:\\www.python.org,环境配置请自己百度。 (1)数据类型分为整数...

  • Python 随手记

    随手记 注意点&、| 是位运算,and、or是逻辑运算and\or 短路计算1.在计算 a and b 时,如果 ...

  • Python 随手记

    1. 拼接路径时,python 用 会根据操作系统的不同拼接出正确的路径。 2. 多行字符串格式化。 例子: 记得...

  • Python基础手记

    Python是一门开箱即用的动态语言,结合搜索引擎和cv大法其威力不可小觑。但很多人(包括我)的Python写的像...

  • 创客关键词-2-Python最

    三、常用开发环境 ●IDLE:Python内置IDE (随python安装包提供) IDLE是python创初人G...

  • 创客关键词-2-Python最新

    三、常用开发环境 ●IDLE:Python内置IDE (随python安装包提供) IDLE是python创初人G...

  • 随拍随想随手记

    花都是香的吗?或许这株花就得了狐臭。 物竞天择竞的也只是异类,而人却是会兄弟阋墙,手足相残的。 孔雀园...

网友评论

      本文标题:Python 随手记

      本文链接:https://www.haomeiwen.com/subject/gxpndctx.html