我有一个文件,它看起来像如何使用Python替换此文本中的冒号?
1::12::33::1555
1::412::1245::23444
等。我需要摆脱最后一个参数,并用逗号替换冒号。我曾尝试:
myfile = open('words.txt', 'r')
content = myfile.read()
content = re.sub(r'(.+)::(.+)::(.+)::(.+)', "\1,\2,\3", content)
myfile = open('words.txt', 'w')
myfile.write(content)
# Close the file
myfile.close()
但后面的参考是不行的,我刚刚结束了与逗号文件..
就是我希望实现的是:
1,12,33
1,412,1245
请添加一些预期的输出。 – 2013-04-30 21:52:22
'my_string.replace(“::”,“,”)'?不会工作? – 2013-04-30 21:52:44
不是Perl,但仍然相关:http://xkcd.com/1171/ – squiguy 2013-04-30 21:56:58