0
對不起,如果標題有點模糊,但我想通過一個列表並且用'/'
替換所有出現的'\\'
。這裏有一個例子:如何使用別的東西替換列表中的所有事件再現
list = ['C:/dir\\file.txt', 'C:/dir\\example.zip', 'C:/dir\\example2.zip']
我想過濾掉所有單個路徑的'\\'
與'/'
可讀性原因。
對不起,如果標題有點模糊,但我想通過一個列表並且用'/'
替換所有出現的'\\'
。這裏有一個例子:如何使用別的東西替換列表中的所有事件再現
list = ['C:/dir\\file.txt', 'C:/dir\\example.zip', 'C:/dir\\example2.zip']
我想過濾掉所有單個路徑的'\\'
與'/'
可讀性原因。
使用str.replace:
>>> 'C:/dir\\file.txt'.replace('\\', '/')
'C:/dir/file.txt'
應用str.replace
到所有的路徑,你會被替換的路徑。
>>> paths = list = ['C:/dir\\file.txt', 'C:/dir\\example.zip', 'C:/dir\\example2.zip']
>>> paths = [path.replace('\\', '/') for path in paths]
>>> paths
['C:/dir/file.txt', 'C:/dir/example.zip', 'C:/dir/example2.zip']
你應該使用os.path
,而不是用手來操作的路徑,但因爲你必須,在這裏你去:
[re.sub("\\\\",'/',x) for x in list]
相當肯定str.replace在Python-3貶值(它甚至不是在該手冊3.3) –
@OliverMatthews,你錯了。 http://docs.python.org/3/library/stdtypes#str.replace – falsetru
啊,我明白了。字符串模塊的替換函數已折舊。我的錯。 –