我的数据位于 .txt 文件中(不,我不能将其更改为其他格式),它看起来像这样:
varaiablename = value
something = thisvalue
youget = the_idea
到目前为止,这是我的代码(取自 Pyparsing 中的示例):
from pyparsing import Word, alphas, alphanums, Literal, restOfLine, OneOrMore, \
empty, Suppress, replaceWith
input = open("text.txt", "r")
src = input.read()
# simple grammar to match #define's
ident = Word(alphas + alphanums + "_")
macroDef = ident.setResultsName("name") + "= " + ident.setResultsName("value") + Literal("#") + restOfLine.setResultsName("desc")
for t,s,e in macroDef.scanString(src):
print t.name,"=", t.value
那么如何告诉我的脚本编辑特定变量的特定值呢?
示例:
我想将变量名的值从 value 更改为 new_value。所以本质上是变量=(我们要编辑的数据)。
我可能应该明确表示我不想直接进入文件并通过将 value 更改为 new_value 来更改值,但我想解析数据,找到变量然后给它一个新值。