【发布时间】:2016-04-06 21:44:53
【问题描述】:
因为&符号是我要处理的文件中的分隔符,所以我试图用 Python 文本文件中的逗号替换所有&符号的实例,但是下面的代码对文件没有影响。
commafile = open('/ampersandfile.txt')
my_file_contents = commafile.read()
commafile.close()
commafile = open('/csvfile.csv', 'w')
commafile.write(my_file_contents.replace('&', '& ').replace(', ', ', '))
commafile.close()
【问题讨论】:
-
您不想用
my_file_contents.replace('&', ',')代替my_file_contents.replace('&', '& ').replace(', ', ', ')并在csvfile.csv 中检查结果吗?