zoukankan      html  css  js  c++  java
  • 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"
  • 相关阅读:
    django 项目需要注意的一些点
    VUE之路
    Oracle 表格碎片的查看方法
    RHEL 6.x or 7.x 使用分区绑定ASM 磁盘的方法
    RMAN 修复主库 nologging 操作导致物理备库的坏块
    Oracle 数据库19c 回退降级到 11.2.0.4 方案
    如何评估oracle 数据库rman全备和增量备份大小
    在将Oracle GI和DB升级到19c或降级到以前的版本之前需要应用的补丁 (Doc ID 2668071.1)
    Oracle 数据库坏块处理
    opatch auto 安装11.2.0.4.20190115 PSU遇到 OUI-67133: Execution of PRE script failed,with returen value 1 报错
  • 原文地址:https://www.cnblogs.com/xjsllll/p/2990264.html
Copyright © 2011-2022 走看看