hoamon's sandbox
hoamon
hoamon's sandbox is about »
tag cloud
- aix
- ajax
- amazon web service
- amd64
- android
- apache
- apple
- athletes
- bank
- baseball
- bike
- bitbucket
- blog
- bond
- book
- boto
- brooks
- bullshit
- capital gains tax
- car accident
- cds
- chrome
- civil engineering
- cloud computing
- cmclass
- computer
- construction management
- construction site management
- django
- drbl
- e-sun bank
- ec2
- eclipse
- ecryptfs
- education
- election
- english
- environment
- fedora
- feeling
- finance
- firefox
- firegpg
- flash ram
- foclass
- football
- fortran
- freetds
- future
- genetic algorithm
- glassfish
- gnuplot
- google adsense
- google app engine
- google gears
- gpg
- graphviz
- gtalk
- hg
- history
- htc
- html5
- http
- https
- ibm
- iconv
- ie
- iis
- iloveit
- imagemagick
- infomation
- investment
- ipatbles
- ipp2p
- iptables
- ironman
- java
- javascript
- job
- jog
- joke
- jpg
- jquery
- jython
- kde
- knapsack problem
- latex
- learning
- least square method
- levenberg-marquardt method
- libsvm
- linux
- list
- lp
- mac
- marathon
- math
- matlab
- mencoder
- mercurial
- microsoft
- mod_fcgi
- mod_python
- modelviz
- moinmoin
- monte carlo
- movie
- mplayer
- msn
- mssql
- mysql
- netbeans
- netfilter
- newton's method
- nfl
- nginx
- odiogo
- offline system
- open source
- openid
- openoffice
- openssl
- option
- oracle
- p2p
- paypal
- pdf2jpg
- pdftojpg
- perl
- personal
- pgp
- php
- policy
- politic
- postgresql
- power
- program
- python
- python25
- r51
- rds
- realty
- reit
- restructured text
- ruby
- ruby on rails
- s3
- science
- screen
- securities
- seediq bale
- self-management
- shell script
- sis
- skype
- solaris
- something
- ssh
- ssl
- subversion
- sun
- swap
- sybase
- tax
- testcase
- the guass-newton method
- the steepest descent method
- thinkpad
- tomcat6
- tortoisehg
- totero
- trac
- travel
- triathlete
- turbogears
- ubuntu
- un*x
- unix
- vedio
- version control
- vim
- virtual machine
- virtualbox
- vst
- web
- windows
- x86_64
- yahoo
- zfs
- zipcode
- zotera
- zotero
» Python 不支援 Big5 擴充字(誤)
"""README: 感謝使徒提姆,我居然天真地以為 Python 沒有作到 Localization 。下面第一段文是我在放屁,請見諒! 就把此文當作是 os.popen 的一段小範例吧! """
有 7 個 Big5 擴充字:碁, 銹, 裏, 墻, 恒, 粧, 嫺,無法透過 Python 的 unicode 函式作轉換。所以我只好呼叫外部 iconv 程式來作轉換。
一開始,我選用 os.system 來作轉換,但 os.system 的所得到的回傳值是 subshell 執行程式的狀態,而不是轉換後的 utf8 碼,後來就改用 os.popen 。
程式範例如下:
try:一開始先嘗試用 unicode 函式轉換,不成功後才使用 os.popen ,並將 stderr 的訊息轉到 stdout 去,如果在得到的回傳值中發現了 iconv: 的字串,則再使用 unicode 函式作『錯誤 ignore 』的轉換。
value = unicode(str(v), 'big5')
except:
value = os.popen('echo "%s"|iconv -f big5 -t utf8 2>&1' % v.replace('`', "'").replace('\n', '')).read().replace('\n', '')
if 'iconv: ' in value: value = unicode(str(v), 'big5', 'ignore')
這樣就能兼顧程式效率及轉換正確率了。







