[ת]Python: python±àÂëÎÊÌâ
ÕâÊÇÒ»¸öÎÒÃÇÔÚ´¦ÀíÖÐÎÄʱ, ¾³£Óöµ½µÄÎÊÌâ.
pythonÀïÃæ»ù±¾ÉÏÒª¿¼ÂÇÈýÖÖ±àÂë¸ñʽ
1 Ô´Îļþ±àÂë
ÔÚÎļþÍ·²¿Ê¹ÓÃcodingÉùÃ÷¡£¸æËßpython½âÊÍÆ÷¸Ã´úÂëÎļþËùʹÓõÄ×Ö·û¼¯¡£
#/usr/bin/python
#coding: utf8
2 ÄÚ²¿±àÂë
´úÂëÎļþÖеÄ×Ö·û´®£¬¾¹ýdecodeÒԺ󣬱»×ª»»ÎªÍ³Ò»µÄunicode¸ñʽµÄÄÚ²¿Êý¾Ý£¬ÀàËÆÓÚu'*'¡£unicodeÊý¾Ý¿ÉÒÔʹÓÃencodeº¯Êý£¬ÔÙ×ÔÓÉת»»ÎªÆäËû¸ñʽµÄÊý¾Ý£¬Ï൱ÓÚÒ»¸öͳһµÄƽ̨¡£
Ö±½ÓÊäÈëunicodeÊý¾Ý
>>> u'ÄãºÃ'
u'\u4f60\u597d'
½«unicodeÊý¾Ýת»»Îªgb2312¸ñʽ
>>> u'ÄãºÃ'.encode('gb2312')
'\xc4\xe3\xba\xc3'
½«ÊäÈëµÄgb2312¸ñʽµÄÊý¾Ý½âÂëΪunicode
>>> 'ÄãºÃ'.decode('gb2312')
u'\u4f60\u597d'
ÊäÈëÊý¾ÝµÄ¸ñʽȡ¾öÓÚËùÓÃshellÖն˵ıàÂëÉèÖ㬱¾ÀýÖÐΪzh_CN
[root@chenzheng python]# echo $LANG
zh_CN
½âÂëͬʱת»»Îªutf8
>>> 'ÄãºÃ'.decode('gb2312').encode('utf8')
'\xe4\xbd\xa0\xe5\xa5\xbd'
3 ÍⲿÊäÈëµÄ±àÂë
ÆäʵÕâ¸öºÍÔÚpython½»»¥shellÖÐÊäÈëµÄ×Ö·û´®£¬ËùÓöµ½µÄÇé¿ö»ù±¾Ò»Ñù¡£µ«³ÌÐòÖг£³£Óõ½´ÓÍøÂ磬Îļþ¶ÁÈ¡µÄÊý¾Ý£¬¹Ê´Ëµ¥¶ÀÁгö£¬ÐèÒªÌØ±ð×¢ÒâÆä±àÂë¸ñʽÊÇ·ñÓÚϵͳҪÇóÏà·û¡£
ÏÂÃæÊÇbaiduÖÐÎı¾ÖܽðÇú°ñµÄÁ´½Ó£¬·µ»ØÒ»¸öxmlÎļþ£¬±àÂë¸ñʽΪgb2312
http://box.zhangmen.baidu.com/x?op=4&listid=1
ÓÉÓÚxml.etree.EelementTree.parse()²»Ê¶±ðgb2312±àÂ룬ÔÚ½âÎöµÄʱºò£¬ÐèÒª½«Æäת»»utf8¸ñʽ²Å¿ÉÒÔ£¬¿ÉÒÔʹÓÃÏÂÃæµÄº¯Êý
def read_page(url):
"""¶ÁÈ¡gb2312±àÂëµÄxmlÎļþ£¬×ª»»Îªutf8¸ñʽ"""
import urllib
udata = urllib.urlopen(url).read().decode('gb2312')
u8data = udata.encode('utf8')
return u8data.replace('gb2312', 'utf-8') #¼òµ¥Ìæ»»xmlÎļþµÚÒ»ÐеÄencodingÊôÐÔÖµ
ÁíÍ⣬¿ÉÒÔʹÓÃÒ»¸öСº¯ÊýÀ´ÅжÏÊý¾ÝµÄ±àÂë¸ñʽ£º
def encoding(s):
cl = ['utf8', 'gb2312']
for a in cl:
try:
s.decode(a)
return a
except UnicodeEncodeError:
pass
return 'unknown'
±¾ÎÄÀ´×ÔCSDN²©¿Í£¬×ªÔØÇë±êÃ÷³ö´¦£ºhttp://blog.csdn.net/jiyucn/archive/2008/02/16/2100006.aspx
Ïà¹ØÎĵµ£º
Script Name: Spider_Create
#Script Label: Create Spider Diagrams
#
#Description: Custom python script developed to create spider diagrams from points contained
#in one or two feature classes. If using two fea ......
ÍøÂçʱ¼ä·þÎñÆ÷ Ò»°ã¶¼×ñÑ RFC868 ÐÒé±ê×¼.
°´¸Ã±ê×¼ ¸½ÏÂÃæ Python Ô´Âë.
# -*- coding: utf-8 -*-
import socket,sys,time
#ʱ¼ä·þÎñÆ÷
host = "stdtime.gov.hk"
#¶Ë¿Ú
port = 37
#Ê±Çø
curtz = 8
#Á¬½Ó·þÎñÆ÷,²¢½ÓÊÕ·µ»Ø
try:
host = socket.gethostbyname(host)
s = socket.socket(socket.AF_INET,soc ......
Posted on 18:40:00 by ÏþÔÂ and filed under Notepad++, Python
¡¡¡¡Notepad++ ÊÇÒ»¸ö¿ªÔ´µÄÎı¾±à¼Æ÷£¬¹¦ÄÜÇ¿´ó¶øÇÒʹÓ÷½±ã¡£±à¼ºÍµ÷ÊÔ Python ³ÌÐòʹÓÃʲô±à¼Æ÷»òÕß IDE ²»Í¬ÈËÓв»Í¬¼û½â¡£ÔÚ²»¿¼ÂÇʹÓõ÷ÊÔ¹¤¾ßµÄÇé¿öÏ£¬Vim »òÕß Emacs Óû§×ÔÈ»¿ÉÒÔÎÞÊÓÆäËû±à¼Æ÷£¬²»¹ýÔÚ Windows »·¾³Ï£¬Ê¹Óà Notepad++ Ö ......
16.1. select — Waiting for I/O completion¶
This module provides access to the select and poll functions available in most operating systems, epoll available on Linux 2.5+ and kqueue available on most BSD. Note that on Windows, it only works for sockets; on other operating systems, it al ......