简体   繁体   English

我如何在 Python 中将字符串列表中的“\\”替换为“\”

[英]How i can replace in a list of strings “\\” with “\” in Python

In my code i have a list of locations and the output of a list is like this在我的代码中,我有一个位置列表,列表的 output 是这样的

['D:\\Todo\\VLC\\Daft_Punk\\One_more_time.mp4"', ...

i want a replace "\\" with "\" (listcancion is a list with all strings) i try to remplace with this code remplacement = [listcancion.replace('\\', '\') for listcancion in listcancion] or this remplacement = [listcancion.replace('\\\\', '\\') for listcancion in listcancion] or also this remplacement = [listcancion.replace('\\', 'X') for listcancion in listcancion] listrandom = [remplacement.replace('X', '\') for remplacement in remplacement]我想用“\”替换“\\”(listcancion 是包含所有字符串的列表)我尝试用此代码remplacement = [listcancion.replace('\\', '\') for listcancion in listcancion]或this remplacement = [listcancion.replace('\\\\', '\\') for listcancion in listcancion]或 this remplacement = [listcancion.replace('\\', 'X') for listcancion in listcancion] listrandom = [remplacement.replace('X', '\') for remplacement in remplacement]

I need to change only the character \ i can't do it things like this ("\\Todo", "\Todo") because i have more characters to remplace.我只需要更改字符 \ 我不能这样做("\\Todo", "\Todo") ,因为我有更多字符要替换。

If i can solved without imports thats great.如果我可以在没有进口的情况下解决,那就太好了。

It is just a matter of string representations.这只是字符串表示的问题。

First, you have to differentiate between a string's "real" content and its representation.首先,您必须区分字符串的“真实”内容及其表示形式。

A string's "real" content might be letters, digits, punctuation and so on, which makes displaying it quite easy.字符串的“真实”内容可能是字母、数字、标点符号等,这使得显示它非常容易。 But imagine a strring which contains a , a line break and a b .但想象一个包含a 、换行符和 a b的字符串。 If you print that string, you get the output如果你打印那个字符串,你会得到 output

a
b

which is what you expect.这是你所期望的。

But in order to make it more compact, this string's representation is a\nb : the line break is represented as \n , the \ serving as an escape character.但是为了使它更紧凑,这个字符串的表示是a\nb :换行符表示为\n\用作转义字符。 Compare the output of print(a) (which is the same as print(str(a)) ) and of print(repr(a)) .比较print(a) (与print(str(a))相同)和print(repr(a))的 output 。

Now, in order not to confuse this with a string which contains a , \ , n and b , a "real" backslash in a string has a representation of \\ and the same string, which prints as a\nb , has a representation of a\\nb in order to distinguish that from the first example.现在,为了不将其与包含a\nb的字符串混淆,字符串中的“真实”反斜杠具有\\的表示形式,而打印为a\nb的相同字符串具有表示形式a\\nb以便与第一个示例区分开来。

If you print a list of anything, it is displayed as a comma-separated list of the representations of their components, even if they are strings.如果您打印任何内容的列表,它会显示为其组件表示的逗号分隔列表,即使它们是字符串。

If you do如果你这样做

for element in listcancion:
    print(element)

you'll see that the string actually contains only one \ where its representation shows \\ .您会看到该字符串实际上只包含一个\ ,其表示形式为\\

(Oh, and BTW, I am not sure that things like [listcancion.<something> for listcancion in listcancion] work as intended; better use another variable as the loop variablen, such as [element.<something> for element in listcancion] .) (哦,顺便说一句,我不确定[listcancion.<something> for listcancion in listcancion]按预期工作;最好使用另一个变量作为循环变量,例如[element.<something> for element in listcancion] .)

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

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