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.).
ÆäµÚÒ»¸ö²ÎÊýΪ¶ÔÏ󣬵ڶþ¸öΪÀàÐÍÃû»òÀàÐÍÃûµÄÒ»¸öÁÐ±í¡£Æä·µ»ØÖµÎª²¼¶ûÐÍ¡£Èô¶ÔÏóµÄÀàÐÍÓë²ÎÊý¶þµÄÀà
Ïà¹ØÎĵµ£º
ÕýÔò±í´ïʽÊǸöħ¹í£¬Ò²ÊǸöÌìʹ¡£ÔÚÄãûÓÐÕÆÎÕËü֮ǰ£¬ËüÊÇħ¹í£¬ÔÚÄãÕÆÎÕËüºó£¬ËüÊÇÌìʹ£¬µ«ÊÇ£¬Ê±
²»Ê±»¹ÊÇÒªÌø³öÀ´£¬¸øÄãµ÷Ƥµ·µ°Ò»·¬¡£
Ò»¸öÕýÔò±í´ïʽ¾ÍÊÇÓÉÆÕͨ×Ö·ûÒÔ¼°ÌØÊâ×Ö·û×é³ÉµÄÎÄ×Öģʽ£¬¸ÃģʽÃèÊöÔÚ²éÕÒÎÄ×ÖÖ÷Ìåʱ´ý
Æ¥ÅäµÄÒ»¸ö»ò¶à¸ö×Ö·û´®¡£ÕýÔò±í´ïʽ×÷Ϊһ¸öÄ£°å£¬½«Ä³¸ö×Ö·ûģʽÓëËùËÑË÷µÄ ......
µ±ÎÒÃÇÕâÑù½¨Á¢Îļþʱ
f =
file('x1.txt', 'w')
f.write(u'ÖÐÎÄ')
f.colse()
Ö±
½Ó½á¹ûÓ¦¸ÃÊÇÀàËÆ
f.write(u'ÖÐÎÄ')
UnicodeEncodeError: 'ascii'
codec can't encode characters in position 0-16: ordinal not in
range(128)
ÒªÖ±½Óд utf-8 ÎļþÔõô°ìÄØ?
import codecs
f = codecs. ......
ÔÚLearning Python 3rd ÀïµÚÈý²¿·ÖÁ·Ï°ÌâÓеÀÌ⣺
1.c ±àдfor Ñ»·£¬¶ÔÓÚ×Ö·û´®S£¬·µ»ØÒ»¸öеÄÁÐ±í£¬ÆäÖаüº¬ÁË×Ö·û´®Ã¿¸ö×Ö·ûµÄASCIIÂë¡£
ÎÒµÄ˼·ÊÇ£º
for i in S:
L = [ord(i) for i in S] #Áбí½âÎöµÄ˼·
ÎҵĴíÎó˼·£º
for i in S:
L = L.append(ord(i))
Ìáʾ¿Õ¶ ......
%a ÐÇÆÚ¼¸µÄ¼òд
%A ÐÇÆÚ¼¸µÄÈ«³Æ
%b Ô·ֵļòд
%B Ô·ݵÄÈ«³Æ
%c ±ê×¼µÄÈÕÆÚµÄʱ¼ä´®
%C
Äê·ÝµÄºóÁ½Î»Êý×Ö
%d Ê®½øÖƱíʾµÄÿÔµĵڼ¸Ìì
%D ÔÂ/Ìì/Äê
%e ÔÚÁ½×Ö·ûÓòÖУ¬Ê®½øÖƱíʾµÄÿÔµĵڼ¸Ìì
%F
Äê-ÔÂ-ÈÕ
%g Äê·ÝµÄºóÁ½Î»Êý×Ö£¬Ê¹ÓûùÓÚÖܵÄÄê
%G Äê·Ö£¬Ê¹ÓûùÓÚÖܵÄÄê
%h ¼òдµÄÔ·ÝÃû ......
>>> import time
>>> import datetime
>>>
now = time.localtime()
>>> now
(2006, 4, 30, 18, 7, 35,
6, 120, 0)
>>> type(now)
<type 'time.struct_time'>
>>>
str_now = time.strftime("%m/%d/%Y %X", now )
>>>
str_n ......