GhostFromHeaven
GhostFromheaven · 共 54 篇文章
【Python】修改文件并立即写回到原始位置(inplace读写)
很多应用多需要处理文件,而处理文件有一个固定的模式:打开文件,读入一些数据,处理这些数据,打印到屏幕上或写入另一个文件。 那么,如果我们想修改之后立即写回文件,该怎么做呢?用什么模式打开?又怎么读写? 我个人尝试了很多中方法,不是无法实现,就是操作非常麻烦。最终放弃。 幸运的是,Python内置模块fileinput就可以轻松完成。代码如下: import fileinput for line in fileinput.input(r"D:\1.txt", inplace=1): print line.replace('d', 'b'), 足够简单吧。(将文件中的‘d’替换成'b')。 关于…
2012-07-31 07:52 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
【Python】二进制文件与Base64编码文本文件转换
前面的话 Python内置的base64模块,在 这里 http://docs.python.org/library/base64.html?highlight=base64#base64 ,包括b64encode,b64decode,urlsafe_b64decode等,可以满足包括URL在内的文本编码需要。但是在用base64.encode编码二进制文件的时候,发现编码不完整,只有部分文件被编码了,base64.decode解码出来文件错误。可能是base64模块用来出来文本的?仔细分析发现,是忘记用二进制模式打开文件了。但是,自己实现base64模块基本功能也不是什么难事,不是要重复发…
2012-07-29 18:06 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
Python二进制文件与十六进制文本文件转换
Python有一个binhex模块,在 http://docs.python.org/library/binhex.html ,用来Encode and decode binhex4 files。我没搞懂binhex4格式,搜索了很久,找到一个讲的相对比较好的 http://www.5dmail.net/html/2006-3-2/200632222823.htm 。 控制欲强的人,对未知或不可控充满恐惧。自己写个二进制与十六进制文件转换也许有点“重复发明轮子”的嫌疑,但是实现起来并没有想象的那么复杂,同时增加可控可用度,还有些意想不到的收获。 ·filehelper 在《 python判断…
2012-07-25 00:14 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
python判断对象是否为文件对象(file object)
方法1:比较type 第一种方法,就是判断对象的 type是否为file ,但该方法对于从file继承而来的 子类不适用 : [python] view plain copy >>> f = open(r"D:\2.zip") >>> type(f) <type 'file'> >>> type(f) == file True >>> class MyFile(file): pass >>> mf = MyFile(r"D:\2.txt") >>> type(mf) <class '__main__.MyFile'> >>> type(mf) == file False 方法2:isinst…
2012-07-20 01:44 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
Python字典按值排序、包含字典的列表按字典值排序的方法
#-*- encoding=utf-8 -*- import operator #按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1)) print sorted_x #[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)] #如果要降序排序,可以指定reverse=True sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=T…
2012-06-19 00:57 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
Python单例模式终极版
如果你真的想使用其他编程语言中类似的“单例模式”,你需要看: http://ghostfromheaven.iteye.com/blog/1562618 但是,我要问的是,Python真的需要单例模式吗?我指像其他编程语言中的单例模式。 答案是: 不需要! 因为,Python有模块(module),最pythonic的单例典范。 模块在在一个应用程序中只有一份,它本身就是单例的,将你所需要的属性和方法,直接暴露在模块中变成模块的全局变量和方法即可! http://stackoverflow.com/a/31887/1447185
2012-06-17 23:16 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
Python单例模式的4种实现方法
#-*- encoding=utf-8 -*- print '----------------------方法1--------------------------' #方法1,实现__new__方法 #并在将一个类的实例绑定到类变量_instance上, #如果cls._instance为None说明该类还没有实例化过,实例化该类,并返回 #如果cls._instance不为None,直接返回cls._instance class Singleton(object): def __new__(cls, *args, **kw): if not hasattr(cls, '_instance…
2012-06-17 22:47 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
Python分段下载文件
下载较大文件时分段下载会加速下载过程,几乎所有下载软件都有类似的特性。在python中如何实现分段下载文件呢? >>> import urllib2 >>> req = urllib2.Request('http://www.python.org/') #下载19000到20000字节的片段 >>> req.headers['Range'] = 'bytes=%s-%s' % (19000, 20000) >>> f = urllib2.urlopen(req) >>> pagerange = f.headers.get('Content-Range') >>> print pagerang…
2012-06-17 20:58 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
Python如何拉平(flatten)嵌套列表(nested list)
有时候会用到嵌套的列表(list),比如 [1, 2, [3, 4, [5, 6]], ["abc", "def"]] 如果将嵌套的列表拉平(flatten)呢?变成: [1, 2, 3, 4, 5, 6, "abc", "def"] 方法有很多,目前了解到的各方面都比较好,也很pythonic的方法是: def flatten(l): for el in l: if hasattr(el, "__iter__") and not isinstance(el, basestring): for sub in flatten(el): yield sub else: yield el l = …
2012-06-15 00:23 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
遍历python字典几种方法
aDict = {'key1':'value1', 'key2':'value2', 'key3':'value3'} print '-----------dict-------------' for d in aDict: print "%s:%s" %(d, aDict[d]) print '-----------item-------------' for (k,v) in aDict.items(): print '%s:%s' %(k, v) #效率最高 print '------------iteritems---------' for k,v in aDict.iteritem…
2012-06-03 19:40 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
Python 迭代器 iteraor
Python迭代器 iterator --by GhostFromHeaven 迭代器是一个实现迭代器协议的容器对象,它基于两个方法: next 返回容器的下一个元素; __iter__ 返回迭代器本身。 迭代器可以通过内置函数iter和一个序列作为参数创建,如下: >>> i = iter('iter') >>> i.next() 'i' >>> i.next() 't' >>> i.next() 'e' >>> i.next() 'r' >>> i.next() Traceback (most recent call last): File "<console>", line 1, in…
2012-06-03 13:06 · Pytho2.x · Python2.7 · GhostFromHeaven · 评论(0)
http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/c
http://www.oracle.com/technetwork/java/javase/downloads/index.html#docs JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe JDK6 API CHM中文参考下载: JDK6API中文参考070114.rar :http://chinesedocument.com/upimg/soft/JDK6API中文参考…
2012-01-27 19:07 · 评论(0)
Ubuntu G++安装
sudo apt-get install make gcc g++ 再装上函数手册 sudo apt-get install manpages-dev 另一方法: sudo apt-get install build-essential 执行完后,完成了gcc,g++,make的安装。 build-essential 是一整套工具,gcc,libc等等
2011-12-28 21:07 · Linux · GhostFromHeaven · 评论(0)
Python多线程Socket程序例子
如果没有multitask包,请从 http://python-multitask.googlecode.com/files/multitask-0.2.0.zip 下载 http://blog.csdn.net/ghostfromheaven/article/details/7061387 server端 from __future__ import with_statement from contextlib import closing import socket import multitask def client_handler(sock): with closing(sock)…
2011-12-11 16:38 · Python2.7 · 评论(0)
Python 多线程简单例子
http://blog.csdn.net/ghostfromheaven/article/details/7039310 import threading import time class MyThread(threading.Thread): def __init__(self, threadnum, max): threading.Thread.__init__(self) self.threadnum = threadnum if (max < 0 ): self.max = 0 else: self.max = max def run(self): for x in xrange(…
2011-12-04 13:58 · Python2.7 · 评论(0)