Python Cookbook (1) 文本

文本啊文本
1. 每个人都同意文本处理很有用。
2. 文本是一串字符,二进制是一串字节。
3. 基本文本操作:
1) 解析数据并将数据放入程序内部的结构中
2) 转成别的形式
3) 生成全新数据
4. 文本的来源
1) 文件
2) 网站
5. 文本应该属于应用程序层面,二进制属于底层

Example:

 1 #!/usr/local/bin/python
 2 import testlib
 3 import string
 4 
 5 #test book <<Python Cookbook>>
 6 
 7 def test_string_template():
 8     new_style = string.Template('this is $thing')
 9     print new_style.substitute({'thing':5})
10     print new_style.substitute({'thing':'test'})
11     # or
12     print new_style.substitute(thing=5)
13     print new_style.substitute(thing='test')
14 
15     
16 def test_string_unicode():
17     unicodestr = u"Hello world"
18     print "unicodestr =", unicodestr
19     utf8str = unicodestr.encode("utf-8")
20     print "utf8str =", utf8str
21     asciistr = unicodestr.encode("ascii")
22     print "asciistr =", asciistr
23     plainstr = unicode(utf8str, "utf-8")
24     print "plainstr =", plainstr
25     
26 def test_string():
27     testlib.in_("test_string"); # from testlib write by self
28     str1="012345"
29     for s in str1 :
30         print s, #do_something_with(c)
31     print "\nstr1="+str1
32     print "str1[1:3] =", str1[1:3]
33     print "str1[0] =", str1[0]
34     print "str1[-2] =", str1[-2]
35     #list_of_lines = one_large_string.splitlines()
36     #one_large_string = '\n'.join(list_of_lines)
37     
38     print "map(ord, 'ciao') =", map(ord, 'ciao')
39     
40     print 'test center, ljust and rjust:'
41     print 'center'.center(20,'+')
42     print '|', 'hej'.ljust(20), '|', 'hej'.rjust(20), '|', 'hej'.center(20), '|'
43     
44     print 'test lstrip, rstrip and strip:'
45     str2='    hej     '
46     print '|', str2.lstrip(), '|', str2.rstrip(), '|', str2.strip(), '|'
47     
48     print 'test upper, lower, capitalize, title:'
49     str3='ONe Two tHree'
50     print "str3 =", str3
51     print "str3.upper =", str3.upper()
52     print "str3.lower =", str3.lower()
53     print "str3.capitalize =", str3.capitalize()
54     print "str3.title =", str3.title()
55     
56     test_string_template()
57     test_string_unicode()
58     
59 def testGo():
60     test_string()
61 
62     
63 if __name__=="__main__" :
64     testGo()

PS:

testlib.py

1 #!/usr/local/bin/python
2 
3 #test tools for all the test
4 
5 def in_(arg):
6     print
7     print arg + " called"
原文地址:https://www.cnblogs.com/xjsllll/p/2990264.html