rep = ''
with open('1.txt', 'r') as fp:
contents = fp.read()
#read()方法讀取所有內容,得到字符串。readline()讀取一行內容,得到字符串。readlines()讀取所有內容,按行得到字符串列表
rep = contents.replace(' ', ',')
with open('1.txt', 'w') as fp2:
#用'w'方式打開文件用於寫入,寫入時原文件的內容會被清空。 python沒有writeline()、writelines()方法
fp2.write(rep)