随笔分类 -  Python

摘要:一.前言 我们在处理小的文本文件时一般使用.read()、.readline() 和 .readlines(),当我们的文件有10个G甚至更大时,用上面的方法内存就直接爆掉了。 二.解决办法 1.看到文件这么大,我们的第一反应都是把文件分割成小块的读取不就好了吗 2.使用with open() 3. 阅读全文
posted @ 2016-10-11 16:07 天外飞仙丶 阅读(15921) 评论(1) 推荐(3)
摘要:1.filter(function,sequence)返回一个 sequence(序列),包括了给定序列中所有调用function(item)后返回值为 true 的元素(如果可能的话,会返回相同的类型)。如果该序列(sequence)是一个string(字符串)或者tuple(元组),返回值必定是... 阅读全文
posted @ 2015-02-12 16:22 天外飞仙丶 阅读(271) 评论(0) 推荐(0)
摘要:主要给出两个版本,一个是通过urllib实现的,另一个是通过urllib2实现的,此为第二种,代码及实例如下:>>> import urllib2>>> from urllib2 import *>>> webUrl = "http://www.baidu.com">>> doc = urllib2... 阅读全文
posted @ 2014-11-24 17:58 天外飞仙丶 阅读(3170) 评论(0) 推荐(0)
摘要:1. 写csv文件# coding: utf-8import csvcsvfile = file('csv_test.csv', 'wb')writer = csv.writer(csvfile)writer.writerow(['姓名', '年龄', '电话']) #写入单行data = [ ... 阅读全文
posted @ 2014-07-10 11:14 天外飞仙丶 阅读(353) 评论(0) 推荐(0)
摘要:官方网站:http://jpype.sourceforge.net/官方使用文档:http://jpype.sourceforge.net/doc/user-guide/userguide.html1.安装 首先在官网上下载程序安装包 ubuntu系统可以直接通过sudo apt-get ins... 阅读全文
posted @ 2014-06-30 18:09 天外飞仙丶 阅读(7931) 评论(0) 推荐(0)
摘要:1、读操作 xlrd 下载地址:https://pypi.python.org/pypi/xlrd 使用代码# encoding : utf-8 #设置编码方式import xlrd #导入xlrd模块#打开指定文件路径的excel文件xlsfi... 阅读全文
posted @ 2014-06-27 12:02 天外飞仙丶 阅读(468) 评论(0) 推荐(0)
摘要:1.scrapy是一个python进行网页爬取的库 文档:http://scrapy-chs.readthedocs.org/zh_CN/latest/index.html 安装:sudo pip easy_install scrapy2.生成项目 scrapy startproject pr... 阅读全文
posted @ 2014-05-12 14:53 天外飞仙丶 阅读(612) 评论(0) 推荐(0)
摘要:1.在使用python进行移位操作的时候发现python进行移位操作后的数值与其他语言不一样(java,php,javascript),然后google了下 看到了这篇帖子http://www.cnblogs.com/zhengyun_ustc/archive/2009/10/14/shifting... 阅读全文
posted @ 2014-05-05 09:50 天外飞仙丶 阅读(10895) 评论(0) 推荐(0)
摘要:1.使用zfill填充str(random.randint(0,99999999)).zfill(8)2.import string"".join(map(lambda x:random.choice(string.digits), range(8)))3.使用random sampleimport randomrandom.sample(["0","1","2","3","4","5","6","7"," 阅读全文
posted @ 2014-04-09 16:29 天外飞仙丶 阅读(9881) 评论(0) 推荐(0)
摘要:最近要用 Python 模拟表单上传文件,搜索了一下常见的解决方案。 如果只是要模拟提交一个不包含文件字段的表单,实现起来是很简单的,但涉及到文件上传就有一点小复杂,需要自己对文件进行编码,或者使用第三方模块。 如果机器上有PycURL,那么可以使用 PycURL 来上传文件。 不过,由于 PycURL 需要用到 curl,在 Windows 下安装可能会有点麻烦,除 PycURL 外,也有一些其它实现 POST 文件上传的方式,比如这儿的 2 楼有人贴出了一个将文件进行编码之后再 POST 的方法,另外还有MultipartPostHandler、urllib2_file、pos... 阅读全文
posted @ 2014-04-03 17:30 天外飞仙丶 阅读(10650) 评论(0) 推荐(0)
摘要:1.def chunks(s, step): return [s[i:i+step] for i in range(0, len(s), step)]chunks(range(50), 10)返回值[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49... 阅读全文
posted @ 2014-04-03 16:42 天外飞仙丶 阅读(3538) 评论(0) 推荐(0)
摘要:在模拟登录一些平台后,在抓取平台数据时需要将cookie设置到urllib2中,python有一个自己的cookielib库,可实现cookie的设置#!/usr/bin/env python#-*- coding: utf-8 -*-import urllib2import cookielib#获取一个保存cookie的对象cj = cookielib.LWPCookieJar()#将一个保存cookie对象,和一个HTTP的cookie的处理器绑定cookie_support = urllib2.HTTPCookieProcessor(cj)#创建一个opener,将保存了cookie的h 阅读全文
posted @ 2014-03-19 15:27 天外飞仙丶 阅读(3226) 评论(0) 推荐(0)
摘要:首先是weiboLogin.py文件,实现一个类。 1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import sys 5 import urllib 6 import urllib2 7 import cookielib 8 import base64 9 import re 10 import json 11 import hashlib 12 import rsa 13 import binascii 14 15 class weiboLogin: 16 cj = cookielib.LWPCo... 阅读全文
posted @ 2014-03-17 11:06 天外飞仙丶 阅读(923) 评论(0) 推荐(0)
摘要:实现登录基本功能,代码略乱,欢迎来喷 大家可以交流下,再改进(分析过程不一一赘述了,直接看源码吧)import hashlibimport stringimport binasciiimport random,reimport urllib,urllib2from datetime import d... 阅读全文
posted @ 2014-03-14 16:04 天外飞仙丶 阅读(3893) 评论(0) 推荐(0)
摘要:用Python在写一个模拟登录程序过程中,有一个JS对字符串加密进行了16进制转换,需要用Python进行同样操作JSvar str = 'e10adc3949ba59abbe56e057f20f883e' //123456 md5 加密串function hexchar2bin(str) { var arr = []; for (var i = 0; i < str.length; i = i + 2) { arr.push("\\x" + str.substr(i, 2)) } arr = arr.join(""... 阅读全文
posted @ 2014-03-14 15:53 天外飞仙丶 阅读(2384) 评论(0) 推荐(0)
摘要:1.combinations(iterable,r) 创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序:官方文档def combinations(iterable, r): # combinations('ABCD', 2) --> AB AC AD BC BD CD # combinations(range(4), 3) --> 012 013 023 123 pool = tuple(iterable) n = len(pool) if r > n: return indice... 阅读全文
posted @ 2014-02-19 16:51 天外飞仙丶 阅读(11729) 评论(0) 推荐(0)
摘要:def f(n): while True: tmp = divmod(n, 2) if tmp[1]!=0: return False if tmp[0]==1: return True n = tmp[0] 阅读全文
posted @ 2014-02-18 16:44 天外飞仙丶 阅读(1253) 评论(0) 推荐(0)
摘要:引自 python官方文档http://docs.python.org/2/library/functions.html range(stop) range(start,stop[,step])This is a versatile function to create lists containing arithmetic progressions. It is most often used inforloops. The arguments must be plain integers. If thestepargument is omitted, it defaults to1. .. 阅读全文
posted @ 2014-02-10 14:39 天外飞仙丶 阅读(729) 评论(0) 推荐(0)
摘要:在Python类的方法中,要调用父类中的某个方法可以这样写class A(object): def __init__(self): print 'A'class B(A): def __init__(self): print 'B' A.__init__(self)b = B()用类名来调用方法,这样一个类的父类发生变化时,子类也要修改,在复杂代码中就会很不方便,super()代码class A(object): def __init__(self): print 'A'class B(A): def __init... 阅读全文
posted @ 2013-11-27 15:42 天外飞仙丶 阅读(384) 评论(0) 推荐(0)
摘要:>>> a = -1>>> a &=0xffffffff>>> a4294967295L 阅读全文
posted @ 2013-11-26 14:14 天外飞仙丶 阅读(10114) 评论(0) 推荐(0)