看起來,在讀取第一行之后,它已經在文件的末尾跳轉以進行寫入,之后就沒有其他行了。我的不是最優雅的解決方案,但它很有效: map_dict = {"variable_1": "20", "variable_2": "15"}handle = open("filename.wbjn", "r+")processed = []for l in handle: for k, v in map_dict.items(): l = l.replace(k, str(v)) processed.append(l)handle.seek(0)handle.write("".join(processed))handle.truncate()handle.close()