来自 ConfigParser 的就地替换
我手头有一个非常棘手的情况(按照我的标准)。 我有一个脚本需要从 ConfigParser 读取脚本变量名称。 例如,我需要读取
self.post.id
.cfg 文件并将其用作脚本中的变量。 我该如何实现这一目标?
我想我的询问不清楚。 .cfg 文件看起来像这样:
[head]
test: me
some variable : self.post.id
这个 self.post.id 将在运行时被替换,从脚本中获取值。
I have a very tricky situation (for my standards) in hand. I have a script that needs to read a script variable name from ConfigParser. For example, I need to read
self.post.id
from a .cfg file and use it as a variable in the script. How do I achieve this?
I suppose I was unclear in my query. The .cfg file looks something like:
[head]
test: me
some variable : self.post.id
This self.post.id is to be replaced at the run time, taking values from the script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试.ini:
Python:
test.ini:
python:
这有点傻。
您拥有一种以源代码形式分发的动态语言。
您正在尝试对源进行相当于更改的操作。 这是易于阅读的纯文本 Python。
为什么不直接更改 Python 源代码并停止搞乱配置文件呢?
拥有这样的代码块要容易得多,
更改它与更改配置文件一样复杂。 而且你的Python程序会更简单、更清晰。
This is a bit silly.
You have a dynamic language, distributed in source form.
You're trying to make what amounts to a change to the source. Which is easy-to-read, plain text Python.
Why not just change the Python source and stop messing about with a configuration file?
It's a lot easier to have a block of code like this
it's just as complex to change this as it is to change a configuration file. And your Python program is simpler and more clear.