寻找一种在脚本中识别和替换Python变量的方法 [英] Looking for a way to identify and replace Python variables in a script

查看:171
本文介绍了寻找一种在脚本中识别和替换Python变量的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可变名字典,我希望这些字符是另一个脚本中的变量名。所以我可能需要识别Python脚本中的所有变量,然后以某种方式从字典中替换所需的变量。然而,我不清楚可能是最优雅的方式来做到这一点。
我必须将一个脚本作为字符串粘贴到替换的另一个脚本吗?任何提示?感谢
编辑:实际问题不是要求一个完整的解决方案,而不是询问是否有可能确定脚本中的哪个字段实际上是一个变量。是否可以找到这些例子,比较它们与PythonBuiltIn变量?!

I have a dictionary of variablenames and I want those to be the variablenames in another script. So I probably need to identify all variables in a Python script and then somehow replace those with the desired ones from the dictionary. Yet I cannot figure out what might be the most elegant way to do that. Do I have to paste the one script as a string to the other script that does the replacement? Any tips? Thanks The actual question is not to request a complete solution rather than asking whether there is a possibility to identify which word in a script is actually a variable. Is it possible to find these eg compare them to PythonBuiltIn variables?!

推荐答案

Python可以读取python。所以写一个脚本与该字典,看看你的第一个脚本,并使用正则表达式替换正确的符号。将文件修改成文件并将其读取,编辑,并将其写回.py文件。

Python can read python. So write a script with that dictionary that looks at your first script and uses regex to replace the correct symbols. Make the file you want to modify an in file and read it, edit it, and write it back to the .py file line by line.

将文件保存在文件I中/ O在python中,以防您需要它:

heres doc on file I/O in python just in case you need it:

http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files

这篇关于寻找一种在脚本中识别和替换Python变量的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆