如何知道一个变量当前是什么类型?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

如何知道一个变量当前是什么类型?

kun xu-3
一个变量可以被赋予任何类型的值,我如何知道当前这个变量是什么类型呢?

a = 11
a = '11'

如何知道a当前是什么类型?

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何知道一个变量当前是什么类型?

Leo Jay
2008/1/15 kun xu <[hidden email]>:
> 一个变量可以被赋予任何类型的值,我如何知道当前这个变量是什么类型呢?
>
> a = 11
> a = '11'
>
> 如何知道a当前是什么类型?
>

>>> def printtype(data):
...     if isinstance(data, int):
...             print 'int'
...     elif isinstance(data, str):
...             print 'str'
...
>>> printtype(11)
int
>>> printtype('11')
str
>>>

--
Best Regards,
Leo Jay
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何知道一个变量当前是什么类型?

罗西
In reply to this post by kun xu-3
type(a)

 
在08-1-15,kun xu <[hidden email]> 写道:
一个变量可以被赋予任何类型的值,我如何知道当前这个变量是什么类型呢?

a = 11
a = '11'

如何知道a当前是什么类型?

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank">http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

如何判断一个文件的编码类型

吴亮
In reply to this post by kun xu-3
请教一下,如何判断一个文件是ASCII嘛呢,还是unicode或者是iso8859-1等等这
些编码
就是相当于linux中的file的部分功能

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese

garcia_relax.vcf (324 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: 如何判断一个文件的编码类型

Jiahua Huang
OT: 估计你又被盖茨的 Notepad.exe 迷惑了,

Notepad.exe 编码类型里的名称是某翻译公司乱翻译的,
它的 ASCII 其实是 GB18030,
而它的所谓 UNICODE 其实是带该死 Bom 头的 UTF-16。


这里给你贴一段简单猜测编码的

encc=''
def zh2utf8(stri):
        """Auto converter encodings to utf8

        It will test utf8,gbk,big5,jp,kr to converter"""
        global encc
        for c in ('utf-8', 'gbk', 'big5', 'jp', 'euc_kr','utf16','utf32'):
                encc = c
                try:
                        return stri.decode(c).encode('utf8')
                except:
                        pass
        encc = 'unk'
        return stri

def zh2unicode(stri):
        """Auto converter encodings to unicode

        It will test utf8,gbk,big5,jp,kr to converter"""
        global encc
        for c in ('utf-8', 'gbk', 'big5', 'jp', 'euc_kr','utf16','utf32'):
                encc = c
                try:
                        return stri.decode(c)
                except:
                        pass
        encc = 'unk'
        return stri


2008/1/15 吴亮 <[hidden email]>:
> 请教一下,如何判断一个文件是ASCII嘛呢,还是unicode或者是iso8859-1等等这
> 些编码
> 就是相当于linux中的file的部分功能
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何判断一个文件的编码类型

吴亮
Jiahua Huang wrote:

> OT: 估计你又被盖茨的 Notepad.exe 迷惑了,
>
> Notepad.exe 编码类型里的名称是某翻译公司乱翻译的,
> 它的 ASCII 其实是 GB18030,
> 而它的所谓 UNICODE 其实是带该死 Bom 头的 UTF-16。
>
>
> 这里给你贴一段简单猜测编码的
>
> encc=''
> def zh2utf8(stri):
>         """Auto converter encodings to utf8
>
>         It will test utf8,gbk,big5,jp,kr to converter"""
>         global encc
>         for c in ('utf-8', 'gbk', 'big5', 'jp', 'euc_kr','utf16','utf32'):
>                 encc = c
>                 try:
>                         return stri.decode(c).encode('utf8')
>                 except:
>                         pass
>         encc = 'unk'
>         return stri
>
> def zh2unicode(stri):
>         """Auto converter encodings to unicode
>
>         It will test utf8,gbk,big5,jp,kr to converter"""
>         global encc
>         for c in ('utf-8', 'gbk', 'big5', 'jp', 'euc_kr','utf16','utf32'):
>                 encc = c
>                 try:
>                         return stri.decode(c)
>                 except:
>                         pass
>         encc = 'unk'
>         return stri
>
>
> 2008/1/15 吴亮 <[hidden email]>:
>  
>> 请教一下,如何判断一个文件是ASCII嘛呢,还是unicode或者是iso8859-1等等这
>> 些编码
>> 就是相当于linux中的file的部分功能
>>
>>    
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
谢谢!
PS:我是用此来比较两个文本的内容的,这两个文本编码可能不一样,所以我得判
断一下它们的编码,然后转换成同一种编码进行比较
我用的是Linux(好久没用过windows了,所以好像和notepad.exe没什么关系吧,
呵呵)

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese

garcia_relax.vcf (324 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: 如何判断一个文件的编码类型

Jiahua Huang
没啥,
只是一般只有用 Notepad 的人才以为 unicode 是编码。

你的需求直接用我贴的代码就可以啦,
管你啥编码的玩意都一律转为 utf8 str 或 unicode

2008/1/15 吴亮 <[hidden email]>:
> 我用的是Linux(好久没用过windows了,所以好像和notepad.exe没什么关系吧,
> 呵呵)
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

请问有没有wxpython的中文教程呀

gary.jsz
In reply to this post by 吴亮
RT,因为E文不咋地,所以想问一下,有没有中文的教程?谢谢!

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何知道一个变量当前是什么类型?

doudou doudou
In reply to this post by 罗西
import types
if type(a)==types.IntType:
    print "Int type"
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何知道一个变量当前是什么类型?

limodou
2008/1/15 doudou doudou <[hidden email]>:
> import types
> if type(a)==types.IntType:
>     print "Int type"

使用types.IntType这种比较是比较老的做法,现在建议使用isinstance(x, type)


--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何知道一个变量当前是什么类型?

Pan Menghan
In reply to this post by kun xu-3
在08-1-15,kun xu <[hidden email]> 写道:
一个变量可以被赋予任何类型的值,我如何知道当前这个变量是什么类型呢?

a = 11
a = '11'

如何知道a当前是什么类型?
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank">


a不是变量而是这个变量的名称, 11或者'11'这才是变量, 就像字典里的key和value的关系一样, a只是key, 而实际的变量是这个11或'11'的value.

这不同于C里面的int a = 11或者char* a = '11', C里面的a就是变量, 因为11或'11'就存储于内存a位置.

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese