Re: [問題] translate的問題
看板Python作者Prius (H-H-H-H-H-H-H-H-H-H-H-H)時間11年前 (2013/07/06 21:31)推噓1(1推 0噓 0→)留言1則, 1人參與討論串2/2 (看更多)
※ 引述《popular10347 (popular)》之銘言:
: hi,我現在用的是python3.3
: 看了一本書example code,用來delete character
: 以下是範例:
: table=maketrans('cs','kz')
: "this is an incredible test".translate(table,' ')
: 想要再轉換character之後刪掉whitespace
: 可是python 的command line 顯示: translate() takes exactly one argument(2 given
: )
: 是哪裡出了問題?
Python 3 以後使用 Unicode
translate() 處理 Unicode 要用新的方式,最簡單的解決方法是改用
bytes
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> table=bytes.maketrans(b'cs',b'kz')
>>> b"this is an incredible test".translate(table,b' ')
b'thizizaninkredibletezt'
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.67.49.104
推
07/07 00:30, , 1F
07/07 00:30, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):