随笔分类 - Python
摘要:第一部分阅读Zen of Python,在Python解析器中输入import this. 一个犀利的Python新手可能会注意到"解析"一词, 认为Python不过是另一门脚本语言. "它肯定很慢!"毫无疑问:Python程序没有编译型语言高效快速. 甚至Python拥护者们会告诉你Python不适合这些领域. 然而,YouTube已用Python服务于每小时4千万视频的请求. 你所要做的就是编写高效的代码和需要时使用外部实现(C/C++)代码. 这里有一些建议,可以帮助你成为一个更好的Python开发者:1.使用内建函数: 你可以用Python写出高
阅读全文
摘要:Unix时间戳转换(python)# -*- coding: utf-8 -*-import timedef timestamp_datetime(value): format = '%Y-%m-%d %H:%M:%S' # value为传入的值为时间戳(整形),如:1332888820 value = time.localtime(value) ## 经过localtime转换后变成 ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, t...
阅读全文
摘要:##Python语言: Hotmail 发信#coding:utf-8from email.mime.text import MIMETextimport smtplibclass Hotmail (object ): def __init__ (self ,account,password): self.account="%s@Hotmail.com" %account self.password=password def send (self ,to,title,content): print self.account,self.pas...
阅读全文
摘要:#######################################把文件每行的tab键分隔符改成逗号分隔符 #######################################from smtplib import SMTP as smtpfrom time import sleepimport linecachestartLine = 1endLine = 517i=startLine#read file contenttry:## fobj=open("renren_45w.txt.txt",'r') fok=open('1
阅读全文
摘要:解析hotmail时遇到类似“广”的编码。解法如下:>>> import html.parser>>> h=html.parser.HTMLParser()>>> print(h.unescape('广告: Hotmail 小技巧:'))广告: Hotmail 小技巧:
阅读全文
摘要:# -*- coding: utf-8 -*-#! /usr/bin/env python3import osls=os.linesep#write filefobj=open('qq.txt','ab+')subject='中文22'subject = (subject+ls).encode('gb18030') #一定要把整个字符串进行转换为bytesfobj.write(subject)fobj.close()print ('Done')
阅读全文
摘要:主要利用random.uniform源代码见http://codemo.qjwm.com/down_3354377.html
阅读全文
摘要:今天在centos 上执行一个python脚本时出现 sh recode command not find错误这个是少了recode或者recode无法执行的缘故#yum install recode解决
阅读全文
摘要:python get百度获得搜索结果 # -*- coding: cp936 -*-import urllib2,urllib,sys,io"""使用GET在百度搜索引擎上查询此例演示如何生成GET串,并进行请求."""url = "http://www.baidu.com/s"search = [('w','codemo')]getString = url + "?" + urllib.urlencode(search)req = urllib2.Request
阅读全文
摘要:把一个py文件编译为pyc文件了。(假设在windows环境下)import py_compilepy_compile.compile(r'H:\game\test.py')py_compile.compile('webmail_sina.py') # Linux环境批量生成pyc文件 >>> import compileall>>> compileall.compile_dir(r'/home/codemo/Desktop/python')
阅读全文
摘要:1. os.linesep是干什么的? os.linesep字符串给出当前平台使用的行终止符。例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。2.解释器行为吧,被解释器引用计数了int会 float不会>>> a=5>>> b=5>>> a is bTrue>>> a=1.1>>> b=1.1>>> a is bFalse3.使用随机数>>> import random>>&g
阅读全文
摘要:对于像'Wed, 11 Apr 2012 09:37:05 +0800'的时间格式化可如下解:>>> date='Wed, 11 Apr 2012 09:37:05 +0800'>>> dd=datetime.datetime.strptime(date,'%a, %d %b %Y %H:%M:%S %z')>>> dd.strftime('%Y-%m-%d %H:%M:%S')Python格式化日期时间的函数为datetime.datetime.strftime();由字符串
阅读全文
摘要:写文件import osls=os.linesep#get filenamefname=raw_input('Enter file name: ')if os.path.exists(fname): print "Error:"+fname #get file contentall=[]print "\nEnter lines ('.' by itself to quit).\n"while True: entry=raw_input('> ') if entry=='.': brea
阅读全文
浙公网安备 33010602011771号