博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
8. 国际化
阅读量:6922 次
发布时间:2019-06-27

本文共 3133 字,大约阅读时间需要 10 分钟。

Python Standard Library

翻译: Python 江湖群

2008-03-28 13:11:54

 

目录

 


 

[index.html 返回首页]

 


 

 

1. 国际化

 


 

 

1.1. locale 模块

locale 模块提供了 C 本地化( localization )函数的接口, 如  所示. 同时提供相关函数, 实现基于当前 locale 设置的数字, 字符串转换. (而 int , float , 以及 string 模块中的相关转换函数不受 locale 设置的影响.)

====Example 8-1. 使用 locale 模块格式化数据=====[eg-8-1]

 

File: locale-example-1.pyimport localeprint "locale", "=>", locale.setlocale(locale.LC_ALL, "")# integer formattingvalue = 4711print locale.format("%d", value, 1), "==",print locale.atoi(locale.format("%d", value, 1))# floating pointvalue = 47.11print locale.format("%f", value, 1), "==",print locale.atof(locale.format("%f", value, 1))info = locale.localeconv()print info["int_curr_symbol"]*B*locale => Swedish_Sweden.12524,711 == 471147,110000 == 47.11SEK*b*

 展示了如何使用 locale 模块获得当前平台 locale 设置.

 

1.1.0.1. Example 8-2. 使用 locale 模块获得当前平台 locale 设置

 

File: locale-example-2.pyimport localelanguage, encoding = locale.getdefaultlocale()print "language", languageprint "encoding", encoding*B*language sv_SEencoding cp1252*b*

 


 

 

1.2. unicodedata 模块

( 2.0 中新增) unicodedata 模块包含了 Unicode 字符的属性, 例如字符类别, 分解数据, 以及数值. 如  所示.

 

1.2.0.1. Example 8-3. 使用 unicodedata 模块

 

File: unicodedata-example-1.pyimport unicodedatafor char in [u"A", u"-", u"1", u"\N{LATIN CAPITAL LETTER O WITH DIAERESIS}"]:    print repr(char),    print unicodedata.category(char),    print repr(unicodedata.decomposition(char)),    print unicodedata.decimal(char, None),    print unicodedata.numeric(char, None)*B*u'A' Lu '' None Noneu'-' Pd '' None Noneu'1' Nd '' 1 1.0u'\303\226' Lu '004F 0308' None None*b*

在 Python 2.0 中缺少 CJK 象形文字和韩语音节的属性. 这影响到了 0x3400-0x4DB5 , 0x4E00-0x9FA5 , 以及 0xAC00- 中的字符, 不过每个区间内的第一个字符属性是正确的, 我们可以把字符映射到起始 实现正常操作:

 

def remap(char):    # fix for broken unicode property database in Python 2.0    c = ord(char)    if 0x3400 <= c <= 0x4DB5:        return unichr(0x3400)    if 0x4E00 <= c <= 0x9FA5:        return unichr(0x4E00)    if 0xAC00 <= c <= 0xD7A3:        return unichr(0xAC00)    return char

Python 2.1 修复了这个 bug .

 


 

 

1.3. ucnhash 模块

(仅适用于 2.0 ) ucnhash 模块为一些 Unicode 字符代码提供了特定的命名. 你可以直接使用 \N{

} 转义符将 Unicode 字符名称映射到字符代码上. 如  所示.

 

1.3.0.1. Example 8-4. 使用 ucnhash 模块

 

File: ucnhash-example-1.py# Python imports this module automatically, when it sees# the first \N{} escape# import ucnhashprint repr(u"\N{FROWN}")print repr(u"\N{SMILE}")print repr(u"\N{SKULL AND CROSSBONES}")*B*u'\u2322'u'\u2323'u'\u2620'*b*

 


 

 

 

PythonStandardLib/chpt8 (2009-12-25 07:15:19由localhost编辑)

 
  • Page.execute = 0.108s  
  • getACL = 0.143s  
  • init = 0.002s  
  • load_multi_cfg = 0.000s  
  • run = 0.735s  
  • send_page = 0.699s  
  • send_page_content = 0.116s  
  • send_page_content|1 = 0.019s  
  • send_page|1 = 0.054s  
  • total = 0.737s

转载于:https://www.cnblogs.com/soft115/archive/2011/11/29/2267612.html

你可能感兴趣的文章
Java操作Redis
查看>>
java.lang.UnsupportedOperationException
查看>>
java 多字段排序
查看>>
我的友情链接
查看>>
XCode快捷键
查看>>
zookeeper错误:KeeperErrorCode = NoChildrenForEphemerals的原因
查看>>
istio-0.8 路由权重设置
查看>>
springmvc数据绑定
查看>>
安装yii2
查看>>
LVS负载均衡之session解决方案 持久连接
查看>>
在商业计算中我们要用java.math.BigDecimal
查看>>
Android基于XMPP Smack Openfire下学习开发IM(五)连接断开重连
查看>>
网络瘫痪分析处理(午饭的一个例子)
查看>>
cisco3750交换机物理端口配置IP地址
查看>>
数据库查询性能需注意几点
查看>>
6 个重构方法可帮你提升 80% 的代码质量
查看>>
[转载]JQUERY的父,子,兄弟节点查找方法
查看>>
10.23 linux任务计划cron 10.24 chkconfig工具 10.25 system
查看>>
正式教你控制win7的任务管理器显示的内容
查看>>
linux--mkfs-tune2fs-mount笔记@1
查看>>