楼主: gafciausa
2231 18

Python Cookbook [推广有奖]

11
Lisrelchen 发表于 2015-9-28 01:21:21 |只看作者 |坛友微信交流群
  1. Filtering a String for a Set of Characters
  2. Credit: Jürgen Hermann, Nick Perkins, Peter Cogolo

  3. Problem
  4. Given a set of characters to keep, you need to build a filtering function that, applied to any string s, returns a copy of s that contains only characters in the set.

  5. Solution
  6. The translate method of string objects is fast and handy for all tasks of this ilk. However, to call translate effectively to solve this recipe’s task, we must do some advance preparation. The first argument to translate is a translation table: in this recipe, we do not want to do any translation, so we must prepare a first argument that specifies “no translation”. The second argument to translate specifies which characters we want to delete: since the task here says that we’re given, instead, a set of characters to keep (i.e., to not delete), we must prepare a second argument that gives the set complement—deleting all characters we must not keep. A closure is the best way to do this advance preparation just once, obtaining a fast filtering function tailored to our exact needs:

  7. import string
  8. # Make a reusable string of all characters, which does double duty
  9. # as a translation table specifying "no translation whatsoever"allchars = string.maketrans('', '')
  10. def makefilter(keep):
  11.     """ Return a function that takes a string and returns a partial copy
  12.         of that string consisting of only the characters in 'keep'.
  13.         Note that `keep' must be a plain string.
  14.     """
  15.     # Make a string of all characters that are not in 'keep': the "set
  16.     # complement" of keep, meaning the string of characters we must delete
  17.     delchars = allchars.translate(allchars, keep)
  18.     # Make and return the desired filtering function (as a closure)
  19.     def thefilter(s):
  20.         return s.translate(allchars, delchars)
  21.     return thefilter
  22. if _ _name_ _ == '_ _main_ _':
  23.     just_vowels = makefilter('aeiouy')
  24.     print just_vowels('four score and seven years ago')
  25. # emits: ouoeaeeyeaao
  26.     print just_vowels('tiger, tiger burning bright')
  27. # emits: ieieuii
复制代码

使用道具

12
Lisrelchen 发表于 2015-9-28 01:22:25 |只看作者 |坛友微信交流群
  1. Checking Whether a String Is Text or Binary
  2. Credit: Andrew Dalke

  3. Problem
  4. Python can use a plain string to hold either text or arbitrary bytes, and you need to determine (heuristically, of course: there can be no precise algorithm for this) which of the two cases holds for a certain string.

  5. Solution
  6. We can use the same heuristic criteria as Perl does, deeming a string binary if it contains any nulls or if more than 30% of its characters have the high bit set (i.e., codes greater than 126) or are strange control codes. We have to code this ourselves, but this also means we easily get to tweak the heuristics for special application needs:

  7. from _ _future_ _ import division           # ensure / does NOT truncate
  8. import string
  9. text_characters = "".join(map(chr, range(32, 127))) + "\n\r\t\b"
  10. _null_trans = string.maketrans("", "")
  11. def istext(s, text_characters=text_characters, threshold=0.30):
  12.     # if s contains any null, it's not text:
  13.     if "\0" in s:
  14.         return False
  15.     # an "empty" string is "text" (arbitrary but reasonable choice):
  16.     if not s:
  17.         return True
  18.     # Get the substring of s made up of non-text characters
  19.     t = s.translate(_null_trans, text_characters)
  20.     # s is 'text' if less than 30% of its characters are non-text ones:
  21.     return len(t)/len(s) <= threshold
复制代码

使用道具

13
Lisrelchen 发表于 2015-9-28 01:27:18 |只看作者 |坛友微信交流群
  1. Controlling Case
  2. Credit: Luther Blissett

  3. Problem
  4. You need to convert a string from uppercase to lowercase, or vice versa.

  5. Solution
  6. That’s what the upper and lower methods of string objects are for. Each takes no arguments and returns a copy of the string in which each letter has been changed to upper- or lowercase, respectively.

  7. big = little.upper( )
  8. little = big.lower( )
复制代码

使用道具

14
Lisrelchen 发表于 2015-9-28 01:29:33 |只看作者 |坛友微信交流群
  1. Accessing Substrings
  2. Credit: Alex Martelli

  3. Problem
  4. You want to access portions of a string. For example, you’ve read a fixed-width record and want to extract the record’s fields.

  5. Solution
  6. Slicing is great, but it only does one field at a time:

  7. afield = theline[3:8]
  8. If you need to think in terms of field lengths, struct.unpack may be appropriate. For example:

  9. import struct
  10. # Get a 5-byte string, skip 3, get two 8-byte strings, then all the rest:
  11. baseformat = "5s 3x 8s 8s"
  12. # by how many bytes does theline exceed the length implied by this
  13. # base-format (24 bytes in this case, but struct.calcsize is general)
  14. numremain = len(theline) - struct.calcsize(baseformat)
  15. # complete the format with the appropriate 's' field, then unpack
  16. format = "%s %ds" % (baseformat, numremain)
  17. l, s1, s2, t = struct.unpack(format, theline)
复制代码

使用道具

15
Lisrelchen 发表于 2015-9-28 01:30:55 |只看作者 |坛友微信交流群
  1. Changing the Indentation of a Multiline String
  2. Credit: Tom Good

  3. Problem
  4. You have a string made up of multiple lines, and you need to build another string from it, adding or removing leading spaces on each line so that the indentation of each line is some absolute number of spaces.

  5. Solution
  6. The methods of string objects are quite handy, and let us write a simple function to perform this task:

  7. def reindent(s, numSpaces):
  8.     leading_space = numSpaces * ' '
  9.     lines = [ leading_space + line.strip( )
  10.               for line in s.splitlines( ) ]
  11.     return '\n'.join(lines)
复制代码

使用道具

16
Lisrelchen 发表于 2015-9-28 01:32:18 |只看作者 |坛友微信交流群
  1. Expanding and Compressing Tabs
  2. Credit: Alex Martelli, David Ascher

  3. Problem
  4. You want to convert tabs in a string to the appropriate number of spaces, or vice versa.

  5. Solution
  6. Changing tabs to the appropriate number of spaces is a reasonably frequent task, easily accomplished with Python strings’ expandtabs method. Because strings are immutable, the method returns a new string object, a modified copy of the original one. However, it’s easy to rebind a string variable name from the original to the modified-copy value:

  7. mystring = mystring.expandtabs( )
  8. This doesn’t change the string object to which mystring originally referred, but it does rebind the name mystring to a newly created string object, a modified copy of mystring in which tabs are expanded into runs of spaces. expandtabs, by default, uses a tab length of 8; you can pass expandtabs an integer argument to use as the tab length.

  9. Changing spaces into tabs is a rare and peculiar need. Compression, if that’s what you’re after, is far better performed in other ways, so Python doesn’t offer a built-in way to “unexpand” spaces into tabs. We can, of course, write our own function for the purpose. String processing tends to be fastest in a split/process/rejoin approach, rather than with repeated overall string transformations:

  10. def unexpand(astring, tablen=8):
  11.     import re
  12.     # split into alternating space and non-space sequences
  13.     pieces = re.split(r'( +)', astring.expandtabs(tablen))
  14.     # keep track of the total length of the string so far
  15.     lensofar = 0
  16.     for i, piece in enumerate(pieces):
  17.         thislen = len(piece)
  18.         lensofar += thislen
  19.         if piece.isspace( ):
  20.             # change each space sequences into tabs+spaces
  21.             numblanks = lensofar % tablen
  22.             numtabs = (thislen-numblanks+tablen-1)/tablen
  23.             pieces[i] = '\t'*numtabs + ' '*numblanks
  24.     return ''.join(pieces)
复制代码

使用道具

17
Nicolle 学生认证  发表于 2015-9-28 01:50:06 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

18
Nicolle 学生认证  发表于 2015-9-28 01:52:20 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

19
frankie1122 在职认证  发表于 2022-1-4 09:13:01 来自手机 |只看作者 |坛友微信交流群
非常好的书,谢谢分享!

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 00:33