[英]define variables in the txt file using python script
i have one .txt file and i want to create python script with sys.argv
or argparse
or other package to define some variables in the txt file and i take some result.我有一个 .txt 文件,我想用
sys.argv
或argparse
或其他包创建 python 脚本来定义 txt 文件中的一些变量,然后我取一些结果。
i want to update the variables in the text file based on arguments passed to the python script我想根据传递给 python 脚本的参数更新文本文件中的变量
txt file maybe like this : txt 文件可能是这样的:
input number 1= %var1%
input number 2= %var2%
result = %vresult(var1-var2)%
must var1,var2 define by the user with some input in the python script and i take result(var1-var2 for ex)必须由用户在 python 脚本中使用一些输入定义 var1,var2,然后我取结果(例如 var1-var2)
i can to do this easy but i dont know how to connect arguments from the python script to txt.我可以很容易地做到这一点,但我不知道如何将参数从 python 脚本连接到 txt。
how can i write a python script to update dynamic variables in the txt file ?如何编写python脚本来更新txt文件中的动态变量?
i dont want with replace i thing so arguments is the better.我不想用替换我的东西所以争论是更好的。
This is a different way using regex
and a dictionary, and taking input using sys.argv
:这是使用
regex
和字典并使用sys.argv
获取输入的不同方式:
import sys,re
v1 = sys.argv[1]
v2 = sys.argv[2]
with open('file.txt','r') as f:
content = f.read()
repl = {'var1':v1, 'var2':v2}
repl_pattern = re.compile(r'(' + '|'.join(repl.keys()) + r')')
final_data = repl_pattern.sub(lambda x: repl[x.group()], content)
with open('file.txt','w') as new:
new.write(final_data)
results:结果:
The file.txt initially had the following content: file.txt 最初具有以下内容:
input number 1= var1
input number 2= var2
result = vresult(var1-var2)
and after running the above code (with: sys.argv[1]=2
and sys.argv[2]
=3) the file changed to:并在运行上述代码(使用:
sys.argv[1]=2
和sys.argv[2]
=3)后,文件更改为:
input number 1= 3
input number 2= 2
result = vresult(3-2)
If you want result = 1
instead of result = vresult(3-2)
then add this code:如果你想要
result = 1
而不是result = vresult(3-2)
然后添加以下代码:
result = str(int(v1)-int(v2))
key3 = 'vresult('+v1+'-'+v2+')'
final_data = final_data.replace(key3,result)
just before the with open('file.txt','w') as new:
....就在
with open('file.txt','w') as new:
....
There's almost certainly a better way to do this but it sounded like an interesting problem so I decided to give it a try.几乎可以肯定有更好的方法来做到这一点,但这听起来像是一个有趣的问题,所以我决定试一试。 I defined a text file with variable names listed as such:
我定义了一个文本文件,其中列出了变量名:
newfile.txt:新文件.txt:
x = 1
y = 2
z = 3
By reading one line at a type and conforming to the "var = value" input format, you can parse the text file for the correct variable then re-write the file with the new value.通过读取一种类型的一行并符合“var = value”输入格式,您可以解析文本文件中的正确变量,然后使用新值重新写入文件。
import fnmatch
var2change = "z = 17"
identifier = var2change.split('=')[0] # Get variable name
# Read file
file = open('newfile.txt', 'r')
fileLines = file.read()
fileLines = fileLines.splitlines()
for lineNum,var in enumerate(fileLines):
if fnmatch.fnmatch(str(var),''.join([identifier,'*'])): break # Found variable to change
file.close()
file = open("newfile.txt", "w")
for ln, data in enumerate(fileLines):
if ln == lineNum:
lineToWrite = ''.join([var2change, '\n'])
else:
lineToWrite = ''.join([data, '\n'])
file.write(lineToWrite)
file.close()
This will re-write the file to read:这将重新写入文件以读取:
newfile.txt:新文件.txt:
x = 1
y = 2
z = 17
parameter.txt file contains: parameter.txt 文件包含:
variable1=10
variable2=20
Below is the python code:下面是python代码:
variable1=0
variable2=0
with open('parameters.txt') as f:
para_list = [i.strip() for i in f.readlines()]
for para in para_list:
exec(para)
print('sum = {}'.format(variable1+variable2))
Below is the result:结果如下:
sum = 30总和 = 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.