Python标准库——一日一库

本文介绍了Python中使用string模块进行文本处理的方法,包括首字母大写、字符替换、模板字符串等实用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        文本(TEXT)处理中最常用的是string、textwrap、re和difflib类。

        今天讲string,不多说,首先贴代码:

import string

s = 'Have a good day.'

print s
print string.capwords(s, None)

leet = string.maketrans('abcdefg', '1234567')

print s
print s.translate(leet)

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

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)
      贴各个函数的解释:

def capwords Found at: string

def capwords(s, sep=None):
    """capwords(s [,sep]) -> string
    
    Split the argument into words using split, capitalize each
    word using capitalize, and join the capitalized words using
    join.  If the optional second argument sep is absent or None,
    runs of whitespace characters are replaced by a single space
    and leading and trailing whitespace are removed, otherwise
    sep is used to split and join the words.
    
    """
    return sep or ' '.join(x.capitalize() for x in s.split(sep))

# Construct a translation string

def maketrans Found at: string

def maketrans(fromstr, tostr):
    """maketrans(frm, to) -> string
    
    Return a translation table (a string of 256 bytes long)
    suitable for use in string.translate.  The strings frm and to
    must be of the same length.
    
    """
    if len(fromstr) != len(tostr):
        raise ValueError, "maketrans arguments must have same length"
    global _idmapL
    if not _idmapL:
        _idmapL = list(_idmap)
    L = _idmapL[:]
    fromstr = map(ord, fromstr)
    for i in range(len(fromstr)):
        L[fromstr[i]] = tostr[i]
    
    return ''.join(L)

####################################################################

str.translate Found at: __builtin__
S.translate(table [,deletechars]) -> string
    
    Return a copy of the string S, where all characters occurring
    in the optional argument deletechars are removed, and the
    remaining characters have been mapped through the given
    translation table, which must be a string of length 256 or None.
    If the table argument is None, no translation is applied and
    the operation simply removes the characters in deletechars.

class Template Found at: string

class Template:
    """A string class for supporting $-substitutions."""
    __metaclass__ = _TemplateMetaclass
    delimiter = '$'
    idpattern = r'[_a-z][_a-z0-9]*'
    def __init__(self, template):
        self.template = template
    
    # Search for $$, $identifier, ${identifier}, and any bare $'s
    def _invalid(self, mo):
        i = mo.start('invalid')
        lines = self.template[:i].splitlines(True)
        if not lines:
            colno = 1
            lineno = 1
        else:
            colno = i - len(''.join(lines[:-1]))
            lineno = len(lines)
        raise ValueError(
            'Invalid placeholder in string: line %d, col %d' % 
             (lineno, colno))
    
    def substitute(self, *args, **kws):
        if len(args) > 1:
            raise TypeError('Too many positional arguments')
        if not args:
            mapping = kws
        elif kws:
            mapping = _multimap(kws, args[0])
        else:
            mapping = args[0]
        # Helper function for .sub()
        def convert(mo): # Check the most common path first.
            named = mo.group('named') or mo.group
             ('braced')
            if named is not None:
                val = mapping[named] # We use this idiom 
                 instead of str() because the latter will
                # fail if val is a Unicode containing non-ASCII 
                 characters.
                return '%s' % (val, )
            if mo.group('escaped') is not None:
                return self.delimiter
            if mo.group('invalid') is not None:
                self._invalid(mo)
            raise ValueError('Unrecognized named group in 
             pattern', self.pattern)
        
        return self.pattern.sub(convert, self.template)
    
    def safe_substitute(self, *args, **kws):
        if len(args) > 1:
            raise TypeError('Too many positional arguments')
        if not args:
            mapping = kws
        elif kws:
            mapping = _multimap(kws, args[0])
        else:
            mapping = args[0]
        # Helper function for .sub()
        def convert(mo):
            named = mo.group('named') or mo.group
             ('braced')
            if named is not None:
                try:
                    return '%s' % (mapping[named], ) # We use 
                     this idiom instead of str() because the latter
            # will fail if val is a Unicode containing non-ASCII
                except KeyError:
                    return mo.group()
            if mo.group('escaped') is not None:
                return self.delimiter
            if mo.group('invalid') is not None:
                return mo.group()
            raise ValueError('Unrecognized named group in 
             pattern', self.pattern)
        
        return self.pattern.sub(convert, self.template)


###########################################
 #########################
# NOTE: Everything below here is deprecated.  Use string 
 methods instead.
# This stuff will go away in Python 3.0.
# Backward compatible names for exceptions

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值