附加带有“\”的特殊字符

编程入门 行业动态 更新时间:2024-10-27 20:24:19
本文介绍了附加带有“\”的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有更好的方法在字符串中附加某些字符,并带有 反斜杠,如下例所示? 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写道: 有没有更好的方法在字符串中附加一个带有反斜杠的字符串,如下例所示? 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

更多推荐

附加带有“\”的特殊字符

本文发布于:2023-11-16 11:56:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特殊字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!