替换几个项目 [英] Replace Several Items

查看:75
本文介绍了替换几个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将字符串中的多个字符替换为仅一个字符。

示例, - ",,。和/什么都没有"

我喜欢这样:

my_string = my_string.replace(" - ","")。replace(" ;. ","")。replace(" /",

"")。replace(")","")。replace("( ","")


但我认为这是一种丑陋的方式。


什么是更好的方法做到了吗?

解决方案

gjhames:


什么是更好的方法这样做?



更好是一个相对术语。如果更好你的意思是更快(在一些

的情况下),那么翻译方法是你的朋友,因为你可以

看到它的第二个参数是要删除的字符。作为第一个参数

你可以使用类似的东西:

"" .join(map(chr,xrange(256)))

如果你的字符串是unicode,你需要一些不同的东西(一个字母

with ñ你想删除的关键字的价值。


再见,

。熊市场


我倾向于像这样使用re模块:


import re

my_string = re.sub(''[\ - ,。/]'' ,'''',my_string)


我希望将我的字符串中的几个字符替换为只有一个。

示例, - ","。"和/什么都没有"

我喜欢这样:

my_string = my_string.replace(" - ","")。replace(" ;. ","")。replace(" /",

"")。replace(")","")。replace("( ","")


但我认为这是一种丑陋的方式。


什么是更好的方法是吗?


Dnia Wed,2008年8月13日09:39:53 -0700(PDT),gjhames napisa3(a):
< blockquote class =post_quotes>
我希望将我的字符串中的几个字符替换为只有一个。

示例, - ",。和/ ;什么都没有"

我喜欢这样:

my_string = my_string.replace(" - ","")。replace(" 。","")。replace(" /",

"")。replace(")","")。replace(") (","")


但我认为这是开始gly方式。


有什么更好的方法呢?



正则表达式可能是最好的方法,

但是如果你真的想使用replace,你也可以使用

循环中的替换方法:


>> somestr =" Qwe.Asd / Zxc()Poi-Lkj"
for in in'' - 。/()'':



.... somestr = somestr.replace(i,'''')

....


>> somestr



''QweAsdZxcPoiLkj''


>>>



下一步是定义你自己的替换功能:


def my_replace( mystr,mychars,myrepl):

"""''mychars''字符串中的每个字符都替换为''myrepl''字符串

in''mystr ''字符串。


示例:


my_replace(''Qwe.Asd / Zxc('',''。/(''' ,''XY'') - ''QweXYAsdXYZxcXY''"""


for my in mychars:

mystr = mystr.replace(我,myrepl)


返回mystr

-

问候,

Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/


I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it''s a ugly way.

What''s the better way to do it?

解决方案

gjhames:

What''s the better way to do it?

Better is a relative term. If with better you mean "faster" (in some
circumstances), then the translate method is your friend, as you can
see its second argument are the chars to be removed. As first argument
you can use something like:
"".join(map(chr, xrange(256)))
If your strings are unicode you will need something different (a dict
with Null values for the key chars you want to remove).

Bye,
bearophile


I tend to use the re module like so :

import re
my_string = re.sub(''[\-,./]'','''',my_string)

I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it''s a ugly way.

What''s the better way to do it?


Dnia Wed, 13 Aug 2008 09:39:53 -0700 (PDT), gjhames napisa3(a):

I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it''s a ugly way.

What''s the better way to do it?

The regular expression is probably the best way to do it,
but if you really want to use replace, you can also use
the replace method in loop:

>>somestr = "Qwe.Asd/Zxc()Poi-Lkj"
for i in ''-./()'':

.... somestr = somestr.replace(i, '''')
....

>>somestr

''QweAsdZxcPoiLkj''

>>>


Next step would be to define your own replacing function:

def my_replace(mystr, mychars, myrepl):
"""Replace every character from ''mychars'' string with ''myrepl'' string
in ''mystr'' string.

Example:

my_replace(''Qwe.Asd/Zxc('', ''./('', ''XY'') -''QweXYAsdXYZxcXY''"""

for i in mychars:
mystr = mystr.replace(i, myrepl)

return mystr
--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/


这篇关于替换几个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆