惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
博客园 - 司徒正美
博客园 - 【当耐特】
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
宝玉的分享
宝玉的分享
V
V2EX
S
SegmentFault 最新的问题
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
L
LangChain Blog
D
Docker
腾讯CDC

hwchiu learning note Blog

Kubernetes 怎麼計算 imageFS | hwchiu learning note Nginx Proxy_Pass 不會重新查詢 DNS | hwchiu learning note Multus 下如何透過 network policy 設定 | hwchiu learning note Linux Bridge MTU | hwchiu learning note Kubevirt 初體驗 | hwchiu learning note [MacOS ]隨手筆記 Sed 與 Rename 的使用 | hwchiu learning note Docusaurus 使用 blog mode 後連結一直反白的問題 | hwchiu learning note gcloud 切換帳號 | hwchiu learning note k8s 內安裝 redis-cluster | hwchiu learning note Helm Chart 中如何根據條件來動態安裝 Template 內的物件 | hwchiu learning note GCS 操作上注意事項 | hwchiu learning note istio 操作記錄 | hwchiu learning note terraform | hwchiu learning note Kubernetes GKE 維運上小筆記 | hwchiu learning note Git 修改 author/committer | hwchiu learning note GCP NAT 相關筆記 | hwchiu learning note gcloud ssh 到 GCP VM | hwchiu learning note CloudSQL 收費注意事項 | hwchiu learning note Loki 安裝上的參數調整以及 Ring 的問題除錯 | hwchiu learning note kustomize + helm | hwchiu learning note 觀測 K8s 內 OOM 事件 | hwchiu learning note GKE 上的 RBAC 筆記 | hwchiu learning note 本地產生 jwt token | hwchiu learning note ArgoCD 安裝筆記 | hwchiu learning note CircleCI Context 的使用 | hwchiu learning note 透過 GCP IAP Gateway 來保護 GKE 內的網站 | hwchiu learning note 閱讀筆記: 「SRE 的工作介绍」 | hwchiu learning note 閱讀筆記: 「DevOps is a failure」 | hwchiu learning note 閱讀筆記: 「面試人生 - 設計一個簡易的分散式 Job Scheduler」 | hwchiu learning note 閱讀筆記: 「Cloudflare 06/21 災後報告」 | hwchiu learning note
Python-translate | hwchiu learning note
HungWei ChiuBlogger · 2013-06-14 · via hwchiu learning note Blog

Python中有個很強大的字串轉換工具 maketrans 跟 translate

str.translate(table[, deletechars]);
Parameters
table -- You can use the maketrans() helper function in the string module to create a translation table.

deletechars -- The list of characters to be removed from the source string.

字串中只要有符合deletechars中的字元都會被刪除,然後剩下的字元就會依照table裡面的mapping來做轉換。

這個mapping的就要利用string.maketrans()來幫忙產生囉,

str.maketrans(intab, outtab]);
Parameters
intab -- This is the string having actual characters.

outtab -- This is the string having corresponding mapping character.

intab跟outtab兩者的長度必須要一樣,會把intab中每一個字元與outtab中相同位置的字元做mapping。

舉例來說

    intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

就會產生一個mapping,把aeiou分別轉換成12345。

    input="abcdefgh"
input = input.translate(trantab)

input就會變成 "1bcd2fgh"

那如果改成

    input="abcdefgh"
input = input.translate(trantab,"fgh")

input就會變成 "1bcd2"

再來個簡單範例,希望能夠把所有的小寫轉成大寫,並把非英文字母外的所有字元都給刪除掉。

#取得所有英文大小寫的集合

    lower = ''.join(map(chr,range(97,123)))
upper = lower.upper()

#創立一個對照表,可以把所有小寫轉成大寫

    ltu = string.maketrans(lower,upper)

#接下來要利用捕集的方式取得非英文字母以外的所有字元,因此就用所有字元-英文字母 #創立一個代表所有字元的字元表

    allchars = string.maketrans('','')

#利用translate的方式,取得所有非英文字母的集合

    delete = allchars.translate(allchars,lower+upper)

#定義一個對應的function,傳入的字串利用ltu跟delete,就能夠把所有非英文字母都刪除,並且小寫轉大寫了。

    def makefilter(input):
print input.translate(ltu,delete)