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)
以上是几种常用的保存文本文件的方法,可以根据具体需求选择适合的方法。