python,修改txt每行部分内容

oldfile = r"C:\Users\haohao\Desktop\precision_log5.txt"
newfile = r"C:\Users\haohao\Desktop\precision_log6.txt"
with open(oldfile, encoding="utf-8", mode='r') as oldfile1:
    with open(newfile, encoding="utf-8", mode='w') as newfile2:
        for i in oldfile1:
            a = i.rstrip("\n").split(",")[1]
            b = str(round(float(a), 2))
            newfile2.write(i.replace(a, b))

 代码作用,用字符b,代替字符a!

你可能感兴趣的:(修改txt文件,图像处理,python)