Python保存文本文件的方法有以下幾種:
file = open("filename.txt", "w")
file.write("Hello, World!")
file.close()
with open("filename.txt", "w") as file:
file.write("Hello, World!")
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open("filename.txt", "w") as file:
file.writelines(lines)
with open("filename.txt", "w") as file:
print("Hello, World!", file=file)
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("filename.txt", data)
以上是幾種常用的保存文本文件的方法,可以根據具體需求選擇適合的方法。