Python 多元赋值方式实现交换两个变量的值

分类:Python

  1. (x, y) = (1, 2) #x = 1, y = 2
  2. (x, y) = (y, x) #x = 2, y = 1

Python:一个简单但实用的文本处理例子

分类:Python文本内容为一行一个单词,如下:
some
are
born
great
some
achieve
greatness
and
some
have
greatness
thrust
upon
them

继续阅读 »

Python下转换时间格式

分类:Python将datetime类型转换成HTTP头所用的GMT时间格式的字符串(如’Thu, 19 Feb 2009 16:00:07 GMT’):
import datetime

  1. GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
  2. datetime.datetime.utcnow().strftime(GMT_FORMAT)

将GMT时间格式的字符串转换成datetime类型:

  1. TIME = 'Thu, 19 Feb 2009 16:00:07 GMT'
  2. datetime.datetime.strptime(TIME, GMT_FORMAT)

继续阅读 »

Python MySQLdb DeprecationWarning问题

分类:Python错误提示:

MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet

解决办法:修改下面2个文件

##### __init__.py###############

#Line 35
#from sets import ImmutableSet
#class DBAPISet(ImmutableSet):
class DBAPISet(frozenset):
继续阅读 »

Python使用动态变量名

分类:Python要写一个程序,让linuxany1=1,linuxany2=2,… inuxany100=100,你会怎么做?
最容易想到的自然是eval,但是实际上根本不需要这种危险的东西,因为Python的变量名就是一个字典的key而已。要获取这个字典,直接用locals和globals函数即可。

  1. >>> names = locals()
  2. >>> for i in xrange(1, 101):
  3. ...   names['linuxany%s' % i] = i
  4. ...
  5. >>> linuxany1
  6. 1
  7. >>> linuxany2
  8. 2
  9. >>> linuxany100
  10. 100

继续阅读 »

Python CGI调试的2两种方法

分类:Python第1种:直接输出标准错误

  1. #linuxany_cgi.py
  2. import sys
  3. sys.stderr = sys.stdout
  4.  
  5. def main():
  6.     import cgi
  7.     # ...do the actual work of the CGI...
  8.     # perhaps ending with:
  9.     print template % script_dictionary
  10.  
  11. print "Content-type: text/html\n\n"
  12. main()

继续阅读 »

Python cgi调试的2种方法

分类:PythonFormatting sprintf()-style in Python

  1. print """<html><head>
  2. <title>%s</title>
  3. </head><body>
  4. <h1>Famous irrational numbers</h1>
  5. <dl><dt>Pi</dt>
  6.     <dd>%2.3f</dd>
  7.     <dt>Square-root of 2</dt>
  8.     <dd>%2.3f</dd></dl>
  9. </body></html>""" % ("linuxany.com-website", 3.1415, 1.4142)

继续阅读 »


返回顶部