只替换列表python中的几个字符

2024-04-28 11:47:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表,其格式如下:

[ '2013,june,25,11,img1.ams.expertcity.com,/builds/g2m/1172/G2M_Mac_x86,84.83.189.112,3', '2013,june,25,11,img1.ams.expertcity.com,/builds/g2m/1172/G2MInstallerExtractor.exe,85.164.14.248,6', '2013,june,25,11,img1.syd.expertcity.com,/builds/g2m/1172/G2MCoreInstExtractor.exe,99.245.80.126,19']

我只需要将列表中的前三个逗号替换为“-”,即列表应如下所示:

^{pr2}$

我试图使用replace,但最终它会将all'、'替换为“-”

^{pr3}$

我不想使用regex,因为列表中的顺序可能会随着时间的推移而变化。 请建议一种更好的方法来做这件事?


Tags: com列表mac格式exex86img1ams
1条回答
网友
1楼 · 发布于 2024-04-28 11:47:27

使用此:^{cd1>}

^{cd2>}有第三个可选参数^{cd3>}。

^{cd2>}上的帮助:

S.replace(old, new[, count]) -> string

Return a copy of string S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

相关问题 更多 >