![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
文章平均质量分 61
语默静喧
这个作者很懒,什么都没留下…
展开
-
python csv文件写操作
import csvimport timedef update_csv2(): for i in range(1): k = open(r'D:\T60\x-ray_0523\Battery\Lot\201305\24\OK-NG-002\WORK_PRI _20130419_00_2_log.csv', 'a') w原创 2013-05-27 15:19:04 · 837 阅读 · 0 评论 -
re模糊查询
import redef testsearchandmatch(): s1="helloworld, i am 30 !" w1 = "world" m1 = re.search(w1, s1) if m1: print("find : %s" % m1.group()) if re.match(w1, s1) ==原创 2013-05-27 15:16:23 · 982 阅读 · 0 评论 -
Python .format(),%格式化时,对字符串编码方式的影响
1、当标准字符串和Unicode字符串在表达式中混合用时,标准字符串将被自动转换为Unicode字符串。例如:s = "hello"t = u"world"w = s + t #w = unicode(s) + t2、当一个字符串方法中使用到Unicode字符串时,结果也总将是Unicode字符串,例如:a = "Hello World"b = a.replace("Worl原创 2014-03-13 09:24:33 · 5964 阅读 · 0 评论 -
python logging 模块,logger.debug(),logger.info()等写入文件时的核心源码
def emit(self, record): """ Emit a record. If a formatter is specified, it is used to format the record. The record is then written to the stream with a trailing newli原创 2014-04-15 16:00:35 · 2348 阅读 · 0 评论 -
Python正则小实例
def re_test(): import re s = "gsmice@sina.cn : 测试账号 : 7568 : 06286ec389c5536914d63d094f93da7a" pattern = re.compile(r"^\s*(?P[^\s:]+)\s*:\s*(?P[^\s:]+)\s*:\s*(?P[^\s:]+)\s*:\s*(?P[^\s:]+)"原创 2014-03-31 20:16:00 · 533 阅读 · 0 评论 -
urllib2源码记录
"""An extensible library for opening URLs using a variety of protocolsThe simplest way to use this module is to call the urlopen function,which accepts a string containing a URL or a Request object翻译 2014-04-25 08:38:30 · 708 阅读 · 0 评论 -
单线程代理无间隔抓取
目标网站:jd目标代理原创 2014-05-18 15:10:03 · 621 阅读 · 0 评论 -
取出list中时间轴时间的数据
def operate_file_list(): repeat_time_param_check_list = [] file_list = [";2014-05-01:7", 'a', 'b', 'c', ';2014-05-08:5', 'd', 'e', 'f', ';2014-06-01:4', 'g',原创 2014-05-12 11:55:55 · 1408 阅读 · 0 评论 -
解析京东页面,生成以该页面为根的所有page_url
def parse_root_page_url(): # page_size_pattern = re.compile(r".*?(\d+)") url = 'http://list.jd.com/737-794-798.html' html = urllib2.urlopen(url).read() page_size_div_pattern = re.compi原创 2014-05-19 14:42:26 · 1639 阅读 · 0 评论 -
Python豆瓣爬虫,指定文件行数写入到文件中
__author__ = 'huafeng'import osimport reimport timeimport codecsimport loggingimport urllib2import randomfrom math import ceilfrom bs4 import BeautifulSoupPATH = os.path.dirname(os.path.ab原创 2014-05-13 15:12:26 · 860 阅读 · 0 评论 -
爬虫关于非规则html处理
html = r'''1/25 <a href=\"?cat=670%2C671%2C672& page=1\" class=\"prev\">上一页<a href=\"? cat=670%2C671%2C672&page=2\" class=\"next\">下一页'''html为网页中解析出的一个原创 2014-05-20 08:25:52 · 1839 阅读 · 0 评论 -
python 求前一天的时间
import datetimenow_time = datetime.datetime.now()print now_timeyesterday = now_time + datetime.timedelta(days=-1)print yesterday.strftime("%Y-%m-%d")原创 2014-07-01 14:28:01 · 978 阅读 · 0 评论 -
python实现线性表顺序存储的插入操作
def insert_list(L, i, element): L_lenght = len(L) if i L_lenght: return False if i <= L_lenght: for k in range(i-1, L_lenght)[::-1]: L[k+1:k+2] = [L[k]]原创 2014-10-30 08:51:33 · 2723 阅读 · 0 评论 -
python实现对数组去重排序操作
l = [1,3,3,4,2,1,3,4,5,6,7,8,6,5,4,2]def sort_(l): if len(l) <= 1: return l mid = l[0] low = [item for item in l if item < mid] high = [item for item in l if item > mid] r原创 2014-10-31 09:13:00 · 4225 阅读 · 0 评论 -
python 斐波那契数列,查找素数,水仙花数字
def fbi(n): '''斐波那契数列''' if n<=2: return 1 else: return fbi(n-1) + fbi(n-2)# print fbi(8)def find_prime(start_num, end_num): '''找出start_num,与end_num之间的素数''' def原创 2014-10-31 14:33:00 · 1755 阅读 · 0 评论 -
python单例模式Singleton
__author__ = 'sivil'#coding:utf-8class Singleton(object): def __new__(cls, *args, **kwargs): if not hasattr(cls, '_instance'): orig = super(Singleton, cls) cls._原创 2015-01-08 09:35:07 · 779 阅读 · 0 评论 -
将文件切割行数相等n个子文件
__author__ = 'huafeng'#coding:utf-8import sysimport osdef cut_file(filename, partial_count=1): with open(filename) as f: line_list = f.readlines() lenght_of_lines = len(line_li原创 2015-01-09 16:40:50 · 504 阅读 · 0 评论 -
python 批量文件名转换
#coding:utf-8import globimport osdef files_rename(file_path_pattern, d): '''批量文件名转换''' file_list= glob.glob(file_path_pattern) for filename in file_list: path, pure_filename = o原创 2015-01-13 18:10:12 · 748 阅读 · 0 评论 -
python爬虫模拟人人网登陆并发表更新状态
网上找了一些资源,python爬虫模拟登陆人人网的例子,但是应该是比较早期的版本,大多已经无法登陆。而且人人网也在做一些信息验证机制也在变化,自己做了一个小爬虫,模拟登陆后在人人网发表自己的状态。登陆是需要的信息: postData = {'email':username,'password':password}发表心情是所需的信息: statusD原创 2014-03-03 08:31:35 · 1261 阅读 · 0 评论 -
python 内置函数map/filter/reduce
Built-in functions==================Let's look in more detail at built-in functions often used with iterators.Two of Python's built-in functions, :func:`map` and :func:`filter`, are somewhatobso翻译 2014-02-14 17:02:31 · 607 阅读 · 0 评论 -
判断数组中是否存在和等于给定值的两个数
给定一个正序排列的数组arr=[1,4,5,7,11,13]和一个整数num=9,判断arr中的是否存在和为num的两个数1、时间复杂度为O(n):def ch_equal(arr, num): start_point = 0;end_point = len(arr) - 1 len_arr = len(arr) com_times = len_arr*(len_a原创 2014-02-14 15:53:32 · 1889 阅读 · 0 评论 -
python 抓取网页网址信息
# -*- coding: cp936 -*-import urllib2from BeautifulSoup import BeautifulSoupimport recontent = urllib2.urlopen('http://www.baidu.com').read()p = re.compile(r'(.*?)')m = p.search(unicode(c原创 2013-07-15 14:39:38 · 912 阅读 · 0 评论 -
用python的BeautifulSoup分析html
序言 之前用python爬取网页的时候,一直用的是regex或者自带的库sgmllib里的SGMLParser。但是遇到复杂一点的情况时,SGMLParser往往就不那么给力了!(哈,难道说我 too native了?毕竟beautifulSoup是继承sgmlparser的么~)所以,我寻寻觅觅寻寻觅觅,发现了BeautifulSoup这么个玩意。BeautifulSoup提供了很人性转载 2013-07-16 08:20:06 · 568 阅读 · 0 评论 -
python组件 tablib剖析
关于实例化的Dataset,像是csv或excel中的一个sheet,你可以往里面放你的数据,设置好headers参数,只要data与headers的列数相同,则tablib就可以按照对应关系将数据添加到文件中。而实例化的Databook则可以认为是一个容器,里面可以放一个或多个sheet,即多个Dataset需放在Databook中data = [('1', '2', ‘3’),('on原创 2013-12-14 11:21:38 · 2329 阅读 · 1 评论 -
python前端生成excel
#前端html: 导出1*************************************************************************************************前端js: $scope.date_from="2013-09-19"; $scope.date_to = "2013-09-22"; $scop原创 2013-12-13 10:49:15 · 1866 阅读 · 0 评论 -
日期类型的问题
var myDate = new Date;year = myDate.getFullYear(); month = "0" + myDate.getMonth(); //月份获得是从第0月开始,而得到的月份中(1--9月不会表示成”01--09“的形式)days = myDate.getDate()当前时间字符串大小进行比较, 起始时间(三个月前,因为月份是从原创 2013-12-15 09:41:48 · 674 阅读 · 0 评论 -
python倒序排列方法梳理
1、数组倒序:原始元素的倒序排列arr = [1,2,3,4,3,4](1)print arr[::-1] ---->[4, 3, 4, 3, 2, 1](2)arr.reverse() print arr ---->[4, 3, 4, 3, 2, 1](3)reversed(arr) #返回一个倒序可遍历对象,需序遍历出arr =原创 2014-01-06 11:35:46 · 66158 阅读 · 2 评论 -
python数据类型list方法整理
1、list() 新建一个空的数组2、list(iterable) 可迭代对象的元素组成的新数组list('hello') ----> ['h', 'e', 'l', 'l', 'o']3、append(...)添加新的元素到数组的最后L.append(object) -- object作为一个元素添加到数组L中arg2 = [1,2,3,4]ar原创 2014-01-06 10:28:23 · 655 阅读 · 0 评论 -
写入csv操作,python自带csv组件
import csvimport timedef update_csv2(): for i in range(1): k = open(r'D:\T60\x-ray_0523\Battery\Lot\201305\24\OK-NG-002\WORK_PRI _20130419_00_2_log.csv', 'a') writer=c原创 2013-12-25 17:24:54 · 899 阅读 · 0 评论 -
将不同目录下的python文件作为module导入
在该python文件所在目录下添加__init__.py文件,使该文件所在的目录作为一个module存在。import syssys.path.join("path")#path是python文件目录添加之后可以直接执行import my_py_module注意:若将my_py_module导入到当前文件中,则my_py_module中所有涉及到os.getcwd()的路径就变为了当前文件所原创 2014-02-11 11:27:37 · 4847 阅读 · 0 评论 -
字符(unicode)转拼音声调数字
根据网上拼音数字转字符资料整理下反向转换。需注意的是,这里的处理类型为string,若为unicode类型需要进行.encode('utf-8')转换#encoding:utf-8# map (final) constanant+tone to tone+constananmapConstTone2ToneConst = {'n1': '1n',原创 2014-02-24 16:29:40 · 4630 阅读 · 1 评论 -
python编码方式
查看编码方式:import chardetprint chardet.detect(str) #str为string[字节序]若写入时以mode='a',encoding='utf-16'方式执行,则会在内容写入前添加标志:‘xff/xfe’若以(mode='wb',encoding='utf-16')方式执行,则不会添加:‘xff/xfe’若以(mode='a',encod原创 2014-02-12 20:37:47 · 1002 阅读 · 0 评论 -
将文件移动到指定目录下,并且加上时间戳进行重命名
def add_timastamp(self): ''' return timestamp mark''' stamp = time.strftime("%Y%m%d%H%M%S",time.localtime()) return(stamp) def rename_file(self,filename_without_timestamp)原创 2014-02-13 13:44:20 · 3135 阅读 · 0 评论 -
python不同数据结构下的if...in... 耗时对比
数据源为txt文件,数据条数:20万,有数据重复出现的情况。#coding=utf8import osimport codecsPATH = r"e:/SVN/chocolate_ime/doc"BASE_FILE = os.path.join(PATH,"HZout_NoTone.txt")#encoding:utf-16CIZU_FILE = os.path.join(PATH,原创 2014-02-25 19:02:48 · 1830 阅读 · 0 评论 -
判断所给字符串是否为对折字符串(peep)
判断一个字符串是否是对折的字符串,类似“peep”就为对折字符串。若“peap”虽然首位相同,但是str[1] =/= str[-2],所以它不是对折字符串写一个函数,判断一个字符串是否是对折字符串,若是,返回True;若不是,则返回Falsedef peep_function(arg): str_len = len(arg) com_times = str_len/2原创 2014-02-13 18:11:09 · 536 阅读 · 0 评论 -
一个数组分为N份
list_lenght = len(new_line_list) file_count = 3 partial_count = list_lenght/file_count for file_index in range(file_count): range_start = partial_count * file_index range_e原创 2015-03-01 21:28:55 · 1673 阅读 · 0 评论