简体   繁体   English

如何从Python2.7中的字符串中删除\\\\'

[英]how to remove \\' from a string in Python2.7

how to remove \\' from a string in Python2.7, i got a string from server: 如何从Python2.7中的字符串中删除\\',我从服务器获得了一个字符串:

'(\\'1\\',\\'YoTWsmjxk4M5lqgmzKzF8oI6owoqJBM5mVumQ-0fL01OLS9gU63Gfw**\\',\\'\\x00\\x00\\x00\\x00\\x94\\xa7\\x2f\\x34\\',\\'\\',\\'2\\');'

and how to transfer to 以及如何转移到

'('1','YoTWsmjxk4M5lqgmzKzF8oI6owoqJBM5mVumQ-0fL01OLS9gU63Gfw**','\\x00\\x00\\x00\\x00\\x94\\xa7\\x2f\\x34','','2')'

Any idea? 任何想法?

str = "'(\\'1\\',\\'YoTWsmjxk4M5lqgmzKzF8oI6owoqJBM5mVumQ-0fL01OLS9gU63Gfw**\\',\\'\\x00\\x00\\x00\\x00\\x94\\xa7\\x2f\\x34\\',\\'\\',\\'2\\');'"

str.replace('\\', '')

use builtin replace method, which takes 2 parameters first 1 is which char you want to replace and the 2nd one is which char you want to replace with. 使用内置的replace方法,该方法需要2个参数,第一个是要替换的char,第二个是要替换的char。

This could work in your case. 这可能适用于您的情况。 If the variable is str, 如果变量为str,

str = '(\\'1\\',\\'YoTWsmjxk4M5lqgmzKzF8oI6owoqJBM5mVumQ-0fL01OLS9gU63Gfw**\\',\\'\\x00\\x00\\x00\\x00\\x94\\xa7\\x2f\\x34\\',\\'\\',\\'2\\');' 

Replace \\\\ with nothing everywhere. 将\\\\替换为任何地方。 But then where ever 'x' is there, you lose the \\\\ preceding the x. 但是,无论哪里有“ x”,您都会丢失x之前的\\\\。

str.replace("\\", "");

But then as you want it before x, you put back \\\\ only before x 但是,如果您希望在x之前添加它,只需在x之前添加\\\\

str.replace("x", "\\x");

You can use builtin replace() module of str . 您可以使用str内置replace()模块。

string = "(\\'1\\',\\'YoTWsmjxk4M5lqgmzKzF8oI6owoqJBM5mVumQ-0fL01OLS9gU63Gfw**\\',\\'\\x00\\x00\\x00\\x00\\x94\\xa7\\x2f\\x34\\',\\'\\',\\'2\\');"

replaced = string.replace("\\'", "'")
print replaced
#Output

"('1','YoTWsmjxk4M5lqgmzKzF8oI6owoqJBM5mVumQ-0fL01OLS9gU63Gfw**','\\x00\\x00\\x00\\x00\\x94\\xa7\\x2f\\x34','','2');"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM