附加带有“\”的特殊字符 [英] append special chars with "\"

查看:125
本文介绍了附加带有“\”的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有更好的方法在字符串中附加某些字符,并带有

反斜杠,如下例所示?


chr ="#$% ^&安培; _ {}" #特殊字符要注意

str =" 123 45 ^& 00 0_ #要转换的字符串

n ="" #init new string

for i in str:

如果我在chr:#如果特殊字符在str

n + =''\ \''#用反斜杠附加它

n + = i

print" old:" ;, str

print" new :,n

谢谢

Tertius

解决方案

%^ &安培; _ {}" #特殊字符要注意

str =" 123 45 ^& 00 0_ #要转换的字符串

n ="" #init new string

for i in str:

如果我在chr:#如果特殊字符在str

n + =''\ \''#用反斜杠附加它

n + = i

print" old:" ;, str

print" new :,n

谢谢

Tertius


tertius写道:
< blockquote class =post_quotes>有没有更好的方法在字符串中附加一个带有
反斜杠的字符串,如下例所示?

chr ="#


%^&安培; _ {}" #特殊字符要注意
str =" 123 45 ^& 00 0_ #要转换的字符串
n ="" #init new string
for str in:
如果我在chr:#如果str中的特殊字符n + =''\\''#用反斜杠附加它
n + = i
print" old:",str
print" new:",n




如果可以的话提供一些先验准备,一次性制作一个字典

,将每个字符(也就是说,它不是由自己表示的
)映射到代表的字符串它。例如,

如果您不可避免地以上面的chr字符串开头,您可以

使字典如下:


charmap = {}

for c in chr:charmap [c] = c +''\\''


你也可以写出dict文字直接,并且在

任何情况下你只需要准备一次这个charmap然后

就可以用它来准备任意数量的翻译。


一旦你有这个charmap,你可以使用它的get方法来获得翻译 - 并准备一个* LIST OF STRINGS *来

加入最后,那个'很多,*很多*比使用+ =字符串上的
a循环更快:


件= [charmap.get( c,c)for c in str]


最后:


n =''''。join(pieces)

Alex


Is there a better way to append certain chars in a string with a
backslash that the example below?

chr = "#$%^&_{}" # special chars to look out for
str = "123 45^ & 00 0_" # string to convert
n = "" # init new string
for i in str:
if i in chr: # if special character in str
n+=''\\'' # append it with a backslash
n+=i
print "old:",str
print "new:",n
Thanks
Tertius

解决方案

%^&_{}" # special chars to look out for
str = "123 45^ & 00 0_" # string to convert
n = "" # init new string
for i in str:
if i in chr: # if special character in str
n+=''\\'' # append it with a backslash
n+=i
print "old:",str
print "new:",n
Thanks
Tertius


tertius wrote:

Is there a better way to append certain chars in a string with a
backslash that the example below?

chr = "#


%^&_{}" # special chars to look out for
str = "123 45^ & 00 0_" # string to convert
n = "" # init new string
for i in str:
if i in chr: # if special character in str
n+=''\\'' # append it with a backslash
n+=i
print "old:",str
print "new:",n



If you can afford some a priori preparation, make a dictionary
once and for all that maps each character (that isn''t to be
represented by itself) to the string that represents it. E.g.,
if you inevitably start with a chr string as above, you can
make the dictionary as follows:

charmap = {}
for c in chr: charmap[c] = c+''\\''

You can also write out the dict literal directly, and in
any case you only need to prepare this charmap once and
can then use it to prepare any number of translations.

Once you have this charmap, you can use its get method to
get the translations -- and prepare a *LIST OF STRINGS* to
be joined up at the end, that''s MUCH, *MUCH* faster than
a loop using += on a string:

pieces = [charmap.get(c,c) for c in str]

and finally:

n = ''''.join(pieces)
Alex


这篇关于附加带有“\”的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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