如何在字符串中进行条件字符替换 [英] How to do conditional character replacement within a string
问题描述
我在Python中有一个unicode字符串,基本上需要逐个字符地进行遍历,并根据规则列表替换某些特定字符串。这样的规则是,如果 a
a 会更改为ö
>在 n
之后。另外,如果连续有两个元音字符,它们将被一个元音字符和:
代替。因此,如果我有字符串 natarook
,什么是获取nötaro:k
?如果重要的话,使用Python 2.6和CherryPy 3.1。
I have a unicode string in Python and basically need to go through, character by character and replace certain ones based on a list of rules. One such rule is that a
is changed to ö
if a
is after n
. Also, if there are two vowel characters in a row, they get replaced by one vowel character and :
. So if I have the string "natarook"
, what is the easiest and most efficient way of getting "nötaro:k"
? Using Python 2.6 and CherryPy 3.1 if that matters.
编辑:连续两个元音的意思是相同的元音(oo,aa,ii)
edit: two vowels in a row does mean the same vowels (oo, aa, ii)
推荐答案
# -*- coding: utf-8 -*-
def subpairs(s, prefix, suffix):
def sub(i, sentinal=object()):
r = prefix.get(s[i:i+2], sentinal)
if r is not sentinal: return r
r = suffix.get(s[i-1:i+1], sentinal)
if r is not sentinal: return r
return s[i]
s = '\0'+s+'\0'
return ''.join(sub(i) for i in xrange(1,len(s)))
vowels = [(v+v, u':') for v in 'aeiou']
prefix = {}
suffix = {'na':u'ö'}
suffix.update(vowels)
print subpairs('natarook', prefix, suffix)
# prints: nötaro:k
prefix = {'na':u'ö'}
suffix = dict(vowels)
print subpairs('natarook', prefix, suffix)
# prints: öataro:k
这篇关于如何在字符串中进行条件字符替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!