Air9

And it's turtles all the way down...

⇝

Air9

And it's turtles all the way down...

Python-Cookbook-1.09 简化 translate 方法

2016-05-25

string.maketrans

Help on built-in function maketrans in module strop:
maketrans(...)
maketrans(frm, to) -> string
Return a translation table (a string of 256 bytes long)
suitable for use in string.translate. The strings frm and to
must be of the same length.
(END)

生成一个供string.translate使用的 ASCII 表,其中frm中的所有字符都依序被替换成to中字符:

>>> maketrans('abc', 'fed')
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`feddefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
>>>

string.translate

Help on function translate in module string:
translate(s, table, deletions='')
translate(s,table [,deletions]) -> string
Return a copy of the string s, where all characters occurring
in the optional argument deletions are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256. The
deletions argument is not allowed for Unicode strings.
(END)

(也可以s.translate(table, deletions='')为格式)

以maketrans生成的映射表为基准进行字符转换:

>>> translate('abcdef', a)
'feddef'
>>> translate('abcdef', a, 'd')
'fedef'
>>> translate('abcdef', a, 'dd')
'fedef'
>>> translate('abcdef', a, 'de')
'fedf'
>>> translate('abcdef', a, 'ade')
'edf'
>>>

自建一个返回闭包的工厂函数 translator

import string
def translator(frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to * len(frm)
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
delete = allchars.translate(allchars, keep.translate(allchars, delete))
def translate(s):
return s.translate(trans, delete)
return translate
if __name__ == '__main__':
digits_only = translator(keep=string.digits)
print digits_only('qwedwefaf24215')
no_digits = translator(delete=string.digits)
print no_digits('qwedwefaf24215')
  • Python
  • cookbook
  • closure
  • factory
  • string
  • Python

扫一扫,分享到微信

微信分享二维码
Permission denied (publickey)
Python-sorted函数中key的用法
© 2018 Air9
Hexo Theme Yilia by Litten
  • ⇝

tag:

  • Privoxy
  • Shadowsocks
  • gfw
  • Coursera
  • JavaScript
  • AngularJS
  • ngResource
  • Python
  • Flask-SQLAlchemy
  • Alembic
  • Flask-Migrate
  • Flask
  • relationship
  • backref
  • Linux
  • x86
  • elf
  • main
  • libc
  • start
  • init
  • gdb
  • sorted
  • key
  • Closure
  • Decorator
  • func_defaults
  • Q
  • asm
  • align
  • capstone
  • ida
  • Spider
  • ThreadPool
  • logging
  • argparse
  • sqlite
  • requests
  • doctest
  • SublimeREPL
  • ExpandRegion
  • Anaconda
  • MarkdownEditing
  • Archlinux
  • pip
  • vmtools
  • bootstrap
  • keyboard
  • tagIndex
  • Brackets
  • gulp
  • browser-sync
  • python
  • ropgadget
  • Refactoring
  • git
  • github
  • ssh
  • hexo
  • rss
  • sitemap
  • idapython
  • idc
  • ionic
  • rop
  • keystone
  • unicorn
  • Mac
  • mongodb
  • MySQL
  • UTF-8
  • markdown
  • evernote
  • sublime
  • vscode
  • proxy
  • iTerm
  • polipo
  • Charles
  • 科学上网
  • Powerline
  • cookbook
  • closure
  • factory
  • string
  • python-magic
  • mime
  • apt-get
  • raspi
  • ANSI
  • termcolor
  • colorama
  • redis
  • brew
  • tmux
  • 字符集
  • 编码
  • ASCII
  • Unicode

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 树莓派初体验

    2018-05-19

    #raspi

  • Markdown 同步 Evernote

    2017-06-01

    #markdown#evernote#sublime#vscode

  • polipo+ss实现 iTerm2 终端科学上网

    2017-04-03

    #Shadowsocks#proxy#iTerm#polipo#Charles#科学上网

  • Linux x86 elf 程序启动过程

    2017-03-16

    #Linux#x86#elf#main#libc#start#init#gdb

  • keystone

    2016-11-30

    #capstone#python#rop#keystone#unicorn

  • Python 控制终端输出颜色 & ANSI 控制码

    2016-10-12

    #Python#ANSI#termcolor#colorama

  • python-magic has no attribute 'from_file'

    2016-10-11

    #Python#pip#python-magic#mime#apt-get

  • idapython-elf-gadget

    2016-10-08

    #ida#python#idapython

  • ionic 使用 popover 时的一点小问题

    2016-09-27

    #JavaScript#AngularJS#ngResource#ionic

  • Code Smell

    2016-09-25

    #Refactoring

  • Mac 安装 mongodb

    2016-09-22

    #Mac#mongodb

  • 命令行调用 idapython 脚本

    2016-09-20

    #ida#python#idapython#idc

  • Mac 下使用 homebrew 安装 redis

    2016-09-12

    #Mac#redis#brew

  • Q 判断 Gadget 时遇到 align 伪指令的问题

    2016-08-23

    #Q#asm#align#capstone#ida

  • Flask-SQLAlchemy 中的 relationship & backref

    2016-08-16

    #Flask-SQLAlchemy#Flask#relationship#backref

  • hexo-rss-sitemap

    2016-07-28

    #hexo#rss#sitemap

  • Python 中的闭包与装饰器

    2016-07-25

    #Python#Closure#Decorator#func_defaults

  • 字符集与编码

    2016-07-06

    #Python#UTF-8#ANSI#字符集#编码#ASCII#Unicode

  • AngularJS-ngResource-save

    2016-07-06

    #Coursera#JavaScript#AngularJS#ngResource

  • brackets-live-preview-gulp

    2016-07-05

    #Coursera#JavaScript#AngularJS#Brackets#gulp#browser-sync

  • bootstrap-keyboard-tagIndex

    2016-06-28

    #Coursera#JavaScript#bootstrap#keyboard#tagIndex

  • Permission denied (publickey)

    2016-06-16

    #git#github#ssh

  • Python-Cookbook-1.09 简化 translate 方法

    2016-05-25

    #Python#cookbook#closure#factory#string

  • Python-sorted函数中key的用法

    2016-05-19

    #Python#sorted#key

  • Flask 数据库更新问题

    2016-05-16

    #Python#Flask-SQLAlchemy#Alembic#Flask-Migrate

  • Spider-07-doctest

    2016-04-08

    #Python#Spider#doctest

  • Mac 安装 MySQL 并设置 utf-8

    2016-04-07

    #MySQL#UTF-8

  • Spider-06-requests

    2016-04-07

    #Python#Spider#requests

  • Spider-05-Spider

    2016-04-06

    #Python#Spider

  • Sublime Preferences

    2016-04-05

    #SublimeREPL#ExpandRegion#Anaconda#MarkdownEditing

  • Spider-04-sqlite3

    2016-04-05

    #Python#Spider#sqlite

  • Spider-03-argparse

    2016-04-04

    #Python#Spider#argparse

  • Spider-02-logging

    2016-04-03

    #Python#Spider#logging

  • Spider-01-MyThreadPool

    2016-04-02

    #Python#Spider#ThreadPool

  • ropgadget capstone

    2016-03-10

    #capstone#python#ropgadget

  • hexo github

    2016-03-09

    #git#github#hexo

  • 10.11 privoxy [Errno 61] Connection refused

    2016-03-09

    #Privoxy#Shadowsocks#gfw

  • arch vmtools

    2016-03-09

    #Archlinux#vmtools

  • arch gdb pip ropgadget

    2016-03-09

    #gdb#Archlinux#pip

  • tmux copy2clipboard

    2016-03-09

    #Archlinux#tmux

  • iTerm下配置Powerline

    2016-03-09

    #iTerm#Powerline