[英]How to update a variable in python 3.6 to that of a string of text in a .txt file
[英]How to create a variable with text file (Python 3.6)
我想为文本文件创建一个变量。 我怎样才能做到这一点?
例如:
with open("somefile.txt")as f:
defaVar = f
我想用它来保存我的程序中的设置,例如:
password = input("some word...,hint:%d", f)
谁能帮我?
例如,如果somefile.txt
包含多行:
abc
def
ghi
你应该做这个:
with open('somefile.txt') as ff:
lines=ff.read().split('\n')
print(lines)
输出应该是:
['abc', 'def', 'ghi']
但是如果你的文件只包含一行:
abc def ghi
您可以改用它:
with open('somefile.txt') as ff:
lines=ff.read()
print(lines)
输出:
abc def ghi
显然,您只想从文件中打印提示文本,为此使用此代码-
with open("somefile.txt")as f:
defaVar = f.read()
password = input("blablabla...,hint: "+defaVar)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.