Python标准库-string模块《未完待续》

简介:

>>> import string

>>> s='hello rollen , how are you '
>>> string.capwords(s)
'Hello Rollen , How Are You'          #每个单词的首字母大写
>>> string.split(s)
['hello', 'rollen', ',', 'how', 'are', 'you']     #划分为列表 默认是以空格划分
>>> s='1+2+3'
>>> string.split(s,'+')                 #以‘+’号进行划分
['1', '2', '3']

maketrans()方法会创建一个能够被translate()使用的翻译表,可以用来改变一些列的字符,这个方法比调用replace()更加的高效。

例如下面的例子将字符串s中的‘a,改为1,‘b’改为2,‘c’改为3’:

>>> leet=string.maketrans('abc','123')
>>> s='abcdef'
>>> s.translate(leet)
'123def'
>>> leet=string.maketrans('abc','123')
>>> s='aAaBcC'
>>> s.translate(leet)
'1A1B3C'

string中的template的小例子:

import string
values = { 'var':'foo' }
t = string.Template("""
Variable : $var
Escape : $$
Variable in text: ${var}iable
""")
print 'TEMPLATE:', t.substitute(values)
s = """
Variable : %(var)s
Escape : %%
Variable in text: %(var)siable
"""
print 'INTERPOLATION:', s % values

上面的例子的输出为:

TEMPLATE: 
Variable : foo 
Escape : $ 
Variable in text: fooiable

INTERPOLATION: 
Variable : foo 
Escape : % 
Variable in text: fooiable

但是上面的substitute如果提供的参数不足的时候,会出现异常,我们可以使用更加安全的办法,如下:

import string
values = { 'var':'foo' }
t = string.Template("$var is here but $missing is not provided")
try:
    print 'substitute() :', t.substitute(values)
except KeyError, err:
    print 'ERROR:', str(err)
print 'safe_substitute():', t.safe_substitute(values)

上面例子的输出为:

substitute() : ERROR: 'missing' 
safe_substitute(): foo is here but $missing is not provided

 

下面来看一些template的高级用法:

import string
template_text = '''
Delimiter : %%
Replaced : %with_underscore
Ignored : %notunderscored
'''
d = { 'with_underscore':'replaced',
'notunderscored':'not replaced',
}
class MyTemplate(string.Template):
    delimiter = '%'
    idpattern = '[a-z]+_[a-z]+'
t = MyTemplate(template_text)
print 'Modified ID pattern:'
print t.safe_substitute(d)

输出为:

Modified ID pattern:

Delimiter : % 
Replaced : replaced 
Ignored : %notunderscored

在这个例子中,我们通过自定义属性delimiter 和 idpattern自定了规则,我们使用%替代了美元符号$,而且我们定义的替换规则是被替换的变量名要包含下环线,所以在上面的例子中,只替换了一个。

 

import textwrap

sample_text = '''
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
'''

print 'No dedent:\n'
print textwrap.fill(sample_text, width=50)



输出为:

No dedent:

The textwrap module can be used to format text 
for output in situations where pretty-printing is 
desired. It offers programmatic functionality 
similar to the paragraph wrapping or filling 
features found in many text editors.

上面的例子设置宽度为50,下面的例子我们来移除缩进

import textwrap

sample_text = '''
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
'''

dedented_text = textwrap.dedent(sample_text)
print 'Dedented:'
print dedented_text




Dedented:

The textwrap module can be used to format text for output in 
situations where pretty-printing is desired. It offers 
programmatic functionality similar to the paragraph wrapping 
or filling features found in many text editors.

Hit any key to close this window...

下面来一个对比:

import textwrap

sample_text = '''
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
'''

dedented_text = textwrap.dedent(sample_text).strip()
for width in [ 45, 70 ]:
    print '%d Columns:\n' % width
    print textwrap.fill(dedented_text, width=width)
    print





上面的例子的输出如下:

45 Columns:

The textwrap module can be used to format
text for output in situations where pretty-
printing is desired. It offers programmatic
functionality similar to the paragraph
wrapping or filling features found in many
text editors.

70 Columns:

The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers programmatic
functionality similar to the paragraph wrapping or filling features
found in many text editors.

Hit any key to close this window...

我们也可以设置首行和剩余的行:

import textwrap

sample_text = '''
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
'''

dedented_text = textwrap.dedent(sample_text).strip()
print textwrap.fill(dedented_text,
initial_indent=' ',
subsequent_indent=' ' * 4,
width=50,
)

输出为:

The textwrap module can be used to format text 
    for output in situations where pretty-printing 
    is desired. It offers programmatic 
    functionality similar to the paragraph 
    wrapping or filling features found in many 
    text editors.

上面的例子设置首行缩进1个空格,其余行缩进4个空格

 

在文本中查找:

import re
pattern = 'this'
text = 'Does this text match the pattern?'
match = re.search(pattern, text)
s = match.start()
e = match.end()
print 'Found "%s"\nin "%s"\nfrom %d to %d ("%s")' % \
(match.re.pattern, match.string, s, e, text[s:e])

start和end返回匹配的位置

输出如下:

Found "this" 
in "Does this text match the pattern?" 
from 5 to 9 ("this")

 

re includes module-level functions for working with regular expressions as text strings, 
but it is more efficient to compile the expressions a program uses frequently. The compile() function converts an expression string into a RegexObject.

import re
# Precompile the patterns
regexes = [ re.compile(p) for p in [ 'this', 'that' ]]
text = 'Does this text match the pattern?'
print 'Text: %r\n' % text
for regex in regexes:
    print 'Seeking "%s" ->' % regex.pattern,
    if regex.search(text):
        print 'match!'
    else:
       print 'no match'

Text: 'Does this text match the pattern?'

Seeking "this" -> match! 
Seeking "that" -> no match 

The module-level functions maintain a cache of compiled expressions. However, 
the size of the cache is limited, and using compiled expressions directly avoids the 
cache lookup overhead. Another advantage of using compiled expressions is that by 
precompiling all expressions when the module is loaded, the compilation work is shifted 
to application start time, instead of to a point when the program may be responding to 
a user action.

 

So far, the example patterns have all used search() to look for single instances of 
literal text strings. The findall() function returns all substrings of the input that 
match the pattern without overlapping.

import re
text = 'abbaaabbbbaaaaa'
pattern = 'ab'
for match in re.findall(pattern, text):
    print 'Found "%s"' % match

Found "ab" 
Found "ab" 

finditer() returns an iterator that produces Match instances instead of the 
strings returned by findall().

import re
text = 'abbaaabbbbaaaaa'
pattern = 'ab'
for match in re.finditer(pattern, text):
    s = match.start()
    e = match.end()
    print 'Found "%s" at %d:%d' % (text[s:e], s, e)

Found "ab" at 0:2 
Found "ab" at 5:7


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/06/05/2537348.html,如需转载请自行联系原作者

相关文章
|
6天前
|
Python
Python Internet 模块
Python Internet 模块。
102 74
|
10天前
|
XML JSON 数据库
Python的标准库
Python的标准库
126 77
|
24天前
|
算法 数据安全/隐私保护 开发者
马特赛特旋转算法:Python的随机模块背后的力量
马特赛特旋转算法是Python `random`模块的核心,由松本真和西村拓士于1997年提出。它基于线性反馈移位寄存器,具有超长周期和高维均匀性,适用于模拟、密码学等领域。Python中通过设置种子值初始化状态数组,经状态更新和输出提取生成随机数,代码简单高效。
104 63
|
26天前
|
测试技术 Python
手动解决Python模块和包依赖冲突的具体步骤是什么?
需要注意的是,手动解决依赖冲突可能需要一定的时间和经验,并且需要谨慎操作,避免引入新的问题。在实际操作中,还可以结合使用其他方法,如虚拟环境等,来更好地管理和解决依赖冲突😉。
|
26天前
|
持续交付 Python
如何在Python中自动解决模块和包的依赖冲突?
完全自动解决所有依赖冲突可能并不总是可行,特别是在复杂的项目中。有时候仍然需要人工干预和判断。自动解决的方法主要是提供辅助和便捷,但不能完全替代人工的分析和决策😉。
|
25天前
|
机器学习/深度学习 算法 数据挖掘
数据分析的 10 个最佳 Python 库
数据分析的 10 个最佳 Python 库
73 4
数据分析的 10 个最佳 Python 库
|
11天前
|
XML JSON 数据库
Python的标准库
Python的标准库
39 11
|
24天前
|
人工智能 API 开发工具
aisuite:吴恩达发布开源Python库,一个接口调用多个大模型
吴恩达发布的开源Python库aisuite,提供了一个统一的接口来调用多个大型语言模型(LLM)服务。支持包括OpenAI、Anthropic、Azure等在内的11个模型平台,简化了多模型管理和测试的工作,促进了人工智能技术的应用和发展。
97 1
aisuite:吴恩达发布开源Python库,一个接口调用多个大模型
|
11天前
|
数据可视化 Python
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
通过这些思维导图和分析说明表,您可以更直观地理解和选择适合的数据可视化图表类型,帮助更有效地展示和分析数据。
54 8
|
19天前
|
安全 API 文件存储
Yagmail邮件发送库:如何用Python实现自动化邮件营销?
本文详细介绍了如何使用Yagmail库实现自动化邮件营销。Yagmail是一个简洁强大的Python库,能简化邮件发送流程,支持文本、HTML邮件及附件发送,适用于数字营销场景。文章涵盖了Yagmail的基本使用、高级功能、案例分析及最佳实践,帮助读者轻松上手。
29 4
下一篇
DataWorks