【发布时间】:2020-03-07 02:00:53
【问题描述】:
我真的很想将一些变量存储在 .txt 文件(或其他文件,如有必要)中,以便即使在关闭 python 终端后也能够再次使用它们。
我还尝试在附加文件之前创建文件: dat = open("data22", "x") 但这并没有解决问题...
n = "hello"
dat = open("data22.txt", "a")
dat.write(n)
dat.close()
我的完整代码在这里:
import colorama
from colorama import init, Fore
colorama.init()
while True:
e = input( "--> ")
while "n" in str(e):
e = e.replace("n", str(n))
print(" ", e)
if e.find("v") == 0:
n = round(((float(e[1:]))**(1/2)), 10)
print(Fore.LIGHTBLUE_EX + str(n) + Fore.RESET)
elif e.find("b") == 0:
n = ((float(e[1:]))**(2))
print(Fore.LIGHTBLUE_EX + str(n) + Fore.RESET)
elif "v" in str(e):
n = round(((float(e[(e.find("v") + 1):]))**(1/(float(e[:(e.find("v"))])))), 10)
print(Fore.LIGHTBLUE_EX + str(n) + Fore.RESET)
elif "b" in str(e):
n = ((float(e[(e.find("b") + 1):]))**(float(e[:(e.find("b"))])))
print(Fore.LIGHTBLUE_EX + str(n) + Fore.RESET)
else:
print(Fore.LIGHTRED_EX + "please define if you want to square or pull the root from your number (%s), by typing n 'v' for root or n 'b' for square..." % e)
print("examples: 3v%s --> (cubic-root of %s)" % (e, e))
print(" 2b%s --> (square of %s)" % (e, e) + Fore.RESET)
dat = open("data22.txt", "a")
dat.write(n)
dat.close()
问题是,文件“data22.txt”甚至没有出现在文件资源管理器中。
【问题讨论】:
-
您是否尝试使用
numpy或Pandas库打开它?
标签: python