[英]Custom table for str.translate in Python 3
If I run this code: 如果我运行此代码:
s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}))
I will get: 我会得到:
ValueError: string keys in translate table must be of length 1
Is there a way to replace multiple characters at once using str.translate
? 有没有办法使用
str.translate
一次替换多个字符? Docs says I can use codecs
for flexible approach, but I can't find out how. Docs说我可以使用
codecs
来实现灵活的方法,但是我不知道怎么做。
If no, what can be done instead then? 如果没有,那该怎么办呢?
No . 不行
str.translate
can be used solely to replace single characters. str.translate
可以单独使用,以取代单独的字符。 The replacement strings can be of any length, but the keys must be a single character. 替换字符串可以是任何长度,但是键必须是单个字符。
When they documentation mentions codecs
they are saying that you can implement a custom encoding, register it and then open the file using it... it's not a matter of calling something like codecs.maketrans
, it's quite some work. 当他们的文档中提到
codecs
他们是说您可以实现自定义编码,进行注册,然后使用该文件打开文件...调用诸如codecs.maketrans
之类的东西并不是一件codecs.maketrans
,这是很多工作。 I'd personally use re.sub
with a function replacement: 我个人将
re.sub
与功能替换一起使用:
replacements = {'as': 'dfg', '1234': 'qw'}
re.sub('({})'.format('|'.join(map(re.escape, replacements.keys()))), lambda m: replacements[m.group()], text)
Which seems to do what you want: 哪个似乎可以满足您的要求:
>>> re.sub('({})'.format('|'.join(map(re.escape, replacements.keys()))), lambda m: replacements[m.group()], "test as other test1234")
'test dfg other testqw'
The translate
method of a string replaces one character by a string according to the translation that you provide. 字符串的
translate
方法根据您提供的翻译用字符串替换一个字符。
Here are a few cases: 以下是几种情况:
Original string: as 1234
Error in [s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}))]
Error in [s = s.translate(str.maketrans({'a': 'd', '12': 'q'}))]
s.translate(str.maketrans({'a': 'd', '1': 'q'})): ds q234
Workaround to get the result 解决方案以获得结果
After the edit of the question, here is a solution to get the desired replacements: 编辑问题之后,以下是获得所需替换的解决方案:
Split by the keys, and then join by the values in your translation dictionary. 按键拆分,然后按翻译词典中的值加入。
Replaced all subsrings: dfg qw
The code: 编码:
s = 'as 1234'
print('Original string:',s)
try:
w = s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}))
print("s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}):", w)
except:
print("Error in [s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}))]")
try:
w = s.translate(str.maketrans({'a': 'd', '12': 'q'}))
print("s.translate(str.maketrans({'a': 'd', '12': 'q'})):", w)
except:
print("Error in [s = s.translate(str.maketrans({'a': 'd', '12': 'q'}))]")
try:
w = s.translate(str.maketrans({'a': 'd', '1': 'q'}))
print("s.translate(str.maketrans({'a': 'd', '1': 'q'})):", w)
except:
print("Error in [s = s.translate(str.maketrans({'a': 'd', '1': 'q'}))]")
trans_dict = {'as': 'dfg', '1234': 'qw'}
for k,v in trans_dict.items():
y = s.split(k)
s = v.join(y)
print('Replaced all subsrings:',s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.