Python3.4 [text]-translate

Source: Internet
Author: User

"1. makestrans () Syntax: str. maketrans (intab, outtab]); the Python maketrans () method is used to create a conversion table for character ing. For the simplest call method that accepts two parameters, the first parameter is a string, represents the character to be converted, and the second parameter is also the string to represent the conversion target. Note: The two strings must have the same length and have a one-to-one relationship. Python3.4 no string. maketrans () is replaced by the built-in function: bytearray. maketrans (), bytes. maketrans (), str. maketrans () "" intab = "abcd" outtab = "1234" str_trantab = str. maketrans (intab, outtab) test_str = "csdn blog: http://blog.csdn.net/wirelessqa" print (test_str.translate (str_trantab) # 3s4n 2log: http://2log.3s4n.net/wirelessq1 "" 2. the usage of translate () is to convert string Characters Based on the table (including 256 characters) given by the parameter table. The characters to be filtered out are placed in the del parameter. Syntax: str. translate (table [, deletechars]); bytes. translate (table [, delete]) bytearray. if the delete parameter is provided for the translate (table [, delete]), delete the delete character in the original bytes, the remaining characters must be mapped according to the table ing given in the table "" # If the table parameter is None, only print (B 'http: // www.csdn.net/wirelessqa'.translate (None, B 'ts ') # B' hp: // www. cdn. ne/wireleqa '# If the table parameter is not NONE, delete the parameter and map it to bytes_tabtrans = bytes. maketrans (B 'hangzhou', B 'abcdefghijklmnopqrstuvwxyz') print (B 'http: // bytes (bytes_tabtrans, B 'ts') # B 'HP: // WWW. CDN. NE/WIRELEQA '"" 3. closure: it is an inner function, referred to by a variable, and this variable is a local variable "" def make_adder (addend) for the function whose outer layer contains it ): def adder (augend): # adder is the inner layer function return augend + addend return adder a = make_adder (1) # generate a closure where addend is 1, note that the return value is adderb = make_adder (2) # generates another closure, and the addend value is 2. Note that the return value is adderprint (a (100), B (100) # a (100) this is equivalent to adder (100). Before adden, it is 1. Therefore, 100 + 1 rows exceed "4. simple encapsulation of the translate method makes it easier to use frm: intabto: outtabdelete: Specify the delete character keep: specify that the reserved character delete and keep overlap, delete first "def my_translator (frm = B'', to = B '', delete = B'', keep = None): if len (to) = 1: to = to * len (frm) # If to has only one character, the number of characters is equal to frm so that it can correspond one by one # Build a ing table trans = bytes. maketrans (frm, to) if keep is not None: # if there is a reserved word allchars = bytes. maketrans (B '', B'') # obtain all the characters keep = keep in the empty ing table. translate (allchars, delete) # Remove the characters contained in the delete from the keep, that is, when the keep and delete are duplicated, the delete = allchars is given priority. translate (allchars, keep) # delete is to remove the keep from all the characters, that is, do not delete the keep # closure def my_translate (s): return s. translate (trans, delete) return my_translate # test my_tranlator # retain only digits digits_only = my_translator (keep = B '000000') print (digits_only (B 'http: // www.csdn.net/wirelessqa 520520 ') # B '000000' # delete All numbers no_digits = my_translator (delete = B '000000') print (no_digits (B' http: // www.csdn.net/wirelessqa 520520 ') # B 'http: // www.csdn.net/wirelessqa' # Use * to replace the number digits_to_hash = my_translator (frm = B' 100', to = B '*') print (digits_to_hash (B 'http: // www.csdn.net/wirelessqa 520520 ') # B' http: // www.csdn.net/wirelessqa ****** '# When delete and keep are duplicated, trans = my_translator (delete = B '20', keep = B '100 ') print (trans (B 'http: // www.csdn.net/wirelessqa 520520 ') # B '55'

Public Account: wirelessqa

About Author:

Author: Bi Peng | old Bi mail: wirelessqa.me@gmail.com

Micro Blog: @ WirelessQA blog: http://blog.csdn.net/wirelessqa




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.