python -修改文件中某一行

写代码写错了顺序,所以想办法把x,y坐标调换回来

def change_ptsxy(fileName):
    fp = open(fileName)
    i = 0
    file_data = ""
    for line in fp.xreadlines():
        # print line.strip("\n")
        TT = line.strip("\n")
        if i > 2 and i < 71:
            # print TT
            TT_temp = TT.split(" ")
            x = int(float(TT_temp[0]))
            y = int(float(TT_temp[1].strip("\r")))
            old_str=str(x)+" "+str(y)
            new_str=str(y)+" "+str(x)
            # print(new_str)
            line = line.replace(old_str, new_str)
            file_data += line
            # print(file_data)
        else:
            file_data += line
        i += 1
    fp = open(fileName, 'w+')

 

posted @ 2017-12-18 17:06  Anita-ff  阅读(6882)  评论(0编辑  收藏  举报