Python Cookbook (1) 文本
2013-03-30 13:05
393 查看
文本啊文本
1. 每个人都同意文本处理很有用。
2. 文本是一串字符,二进制是一串字节。
3. 基本文本操作:
1) 解析数据并将数据放入程序内部的结构中
2) 转成别的形式
3) 生成全新数据
4. 文本的来源
1) 文件
2) 网站
5. 文本应该属于应用程序层面,二进制属于底层
Example:
PS:
testlib.py
1. 每个人都同意文本处理很有用。
2. 文本是一串字符,二进制是一串字节。
3. 基本文本操作:
1) 解析数据并将数据放入程序内部的结构中
2) 转成别的形式
3) 生成全新数据
4. 文本的来源
1) 文件
2) 网站
5. 文本应该属于应用程序层面,二进制属于底层
Example:
#!/usr/local/bin/python import testlib import string #test book <<Python Cookbook>> def test_string_template(): new_style = string.Template('this is $thing') print new_style.substitute({'thing':5}) print new_style.substitute({'thing':'test'}) # or print new_style.substitute(thing=5) print new_style.substitute(thing='test') def test_string_unicode(): unicodestr = u"Hello world" print "unicodestr =", unicodestr utf8str = unicodestr.encode("utf-8") print "utf8str =", utf8str asciistr = unicodestr.encode("ascii") print "asciistr =", asciistr plainstr = unicode(utf8str, "utf-8") print "plainstr =", plainstr def test_string(): testlib.in_("test_string"); # from testlib write by self str1="012345" for s in str1 : print s, #do_something_with(c) print "\nstr1="+str1 print "str1[1:3] =", str1[1:3] print "str1[0] =", str1[0] print "str1[-2] =", str1[-2] #list_of_lines = one_large_string.splitlines() #one_large_string = '\n'.join(list_of_lines) print "map(ord, 'ciao') =", map(ord, 'ciao') print 'test center, ljust and rjust:' print 'center'.center(20,'+') print '|', 'hej'.ljust(20), '|', 'hej'.rjust(20), '|', 'hej'.center(20), '|' print 'test lstrip, rstrip and strip:' str2=' hej ' print '|', str2.lstrip(), '|', str2.rstrip(), '|', str2.strip(), '|' print 'test upper, lower, capitalize, title:' str3='ONe Two tHree' print "str3 =", str3 print "str3.upper =", str3.upper() print "str3.lower =", str3.lower() print "str3.capitalize =", str3.capitalize() print "str3.title =", str3.title() test_string_template() test_string_unicode() def testGo(): test_string() if __name__=="__main__" : testGo()
PS:
testlib.py
#!/usr/local/bin/python #test tools for all the test def in_(arg): print print arg + " called"
相关文章推荐
- 【python cookbook】改变多行文本字符串的缩进
- python cookbook:第二章 字符串和文本
- 基于《PythonCookbook》的学习(4)—— 文本模式的匹配和查找
- python cookbook第三版学习笔记四:文本以及字符串令牌解析
- python 探索(三) Python CookBook 文本处理 笔记
- Python cookbook(字符串与文本)在字符串的开头或结尾处进行文本匹配操作
- 基于《PythonCookbook》的学习(5)—— 查找和替换文本
- 【python cookbook】python检查一个字符串是文本还是二进制
- python cookbook 字符串和文本
- Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作示例
- 基于《PythonCookbook》的学习(2)——在字符串的卡头或结尾做文本匹配
- 基于《PythonCookbook》的学习(6)——以不区分大小写的方式对文本做查找和替换
- python cookbook第三版学习笔记二:字典
- 开始学习《python cookbook》
- 基于《PythonCookbook》的学习(3)——利用 Shell 通配符做字符串匹配
- 《Modern Python Cookbook》(Python编程范例)笔记1.2 命名
- Python Cookbook 第二版 汉化版 [00-2-Preface] Part 1
- 1.[python cookbook]简单EchoServer
- Recipe 1.3. Testing Whether an Object Is String-like(Python Cookbook)
- Using scatter plots for multivariate data —— python data science cookbook