易截截图软件、单文件、免安装、纯绿色、仅160KB

python中如何判断一个变量的数据类型?(原创)

import types
type(x) is types.IntType # 判断是否int 类型
type(x) is types.StringType #是否string类型
.........
--------------------------------------------------------
超级恶心的模式,不用记住types.StringType
import types
type(x) == types(1) # 判断是否int 类型
type(x) == type('a') #是否string类型
------------------------------------------------------
使用内嵌函数:
isinstance
(
object, classinfo
)
Return true if the object
argument is an instance
of the classinfo
argument, or of a (direct or indirect)
subclass thereof. Also return true if classinfo
is a type
object and object
is an object of that type. If object
is not a class instance or an object of the given type, the function
always returns false. If classinfo
is neither a class
object nor a type object, it may be a tuple of class or type objects,
or may recursively contain other such tuples (other sequence types are
not accepted). If classinfo
is not a class, type, or
tuple of classes, types, and such tuples, a TypeError
exception is raised. Changed in version
2.2: Support for a tuple of type information was added.
Python可以得到一个对象的类型 ,利用type函数:
>>>lst = [1, 2, 3]
>>>type(lst)
<type 'list'>
不仅如此,还可以利用isinstance函数,来判断一个对象是否是一个已知的类型。
isinstance说明如下:
    isinstance(object, class-or-type-or-tuple) -> bool
   
    Return whether an object is an instance of a class or of a subclass
thereof.
    With a type as second argument, return whether that is the object's
type.
   
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut
for
    isinstance(x, A) or isinstance(x, B) or ... (etc.).
其第一个参数为对象,第二个为类型名或类型名的一个列表。其返回值为布尔型。若对象的类型与参数二的类


相关文档:

使用Python读SEGY道头

自己遇到的一个问题, WestGeco的3D地震数据, 提取导航数据:
#!/bin/env python
import sys
import struct
try:
        f=open(sys.argv[1],'rb')
except (IOError,Exception):
    print '''usage:
        scriptname segyfilename
'''
&nbs ......

python中package机制的两种实现方式


当执行import
module时,解释器会根据下面的搜索路径,搜索module1.py文件。
1) 当前工作目录
2) PYTHONPATH中的目录
3) Python安装目录
(/usr/local/lib/python)
事实上,模块搜索是在保存在sys.path这个全局变量中的目录列表中进行搜索。
sys.path会在解释器开始执行时被初始化成包含:
1)当前工作目录
2) PYT ......

用 Python 3 写的命令行百度词典

今天是第二天自己看关于Python了,看见一个Python2写的百度词典,我也用Python 3 写了一个。真的很小巧,呵呵,很好的语言。
不知道怎么上传代码格式的,就上传文本了:
# -*- coding: utf8 -*-
import urllib.parse
import urllib.request
def search(word):
    #word = input("输入你要查询的 ......

Python正则表达式

正则表达式是个魔鬼,也是个天使。在你没有掌握它之前,它是魔鬼,在你掌握它后,它是天使,但是,时
不时还是要跳出来,给你调皮捣蛋一番。


一个正则表达式就是由普通字符以及特殊字符组成的文字模式,该模式描述在查找文字主体时待
匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的 ......

Python MySQLdb 查询返回字典结构


Python MySQLdb 查询返回字典结构 smallfish
MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.DictCursor就行。
默认程序:
import MySQLdb
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', d ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号