def updateFile(file,old_str,new_str):
with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
for line in f1:
f2.write(re.sub(old_str,new_str,line))
os.remove(file)
os.rename("%s.bak" % file, file)
updateFile(r"D:\zdz\myfile.txt", "zdz", "daziran")#将"D:\zdz\"路径的myfile.txt文件把所有的zdz改为daziran
## 绘制该文件中的数据
## 需要引入pylab库,里面用到的函数和MATLAB里的非常类似
def plotData(X, y):
length = len(y)
pylab.figure(1)
pylab.plot(X, y, 'rx')
pylab.xlabel('Population of City in 10,000s')
pylab.ylabel('Profit in $10,000s')
pylab.show()#让绘制的图像在屏幕上显示出来