简体   繁体   English

读取文件并提取数据并从 python 文件中分配给变量

[英]Read a file and extract data and assign to a variable from a python file

I am trying to extract BINPATH, LIBPATH,CPPPATH from a conan.txt file which looks like:我正在尝试从 conan.txt 文件中提取 BINPATH、LIBPATH、CPPPATH,该文件如下所示:

conan = {

    "conan" : {
        "CPPPATH"     : ['something'],
        "BINPATH"     : ['something'],
        "LIBS"        : ['something'],
        "CPPDEFINES"  : [],
        "CXXFLAGS"    : [],
        "CCFLAGS"     : [],
        "SHLINKFLAGS" : [],
        "LINKFLAGS"   : [],
    },
    "conan_version" : "None",

    "boost" : {
        "CPPPATH"     : ['C:\\.conan\\123456\\1\\include'],
        "LIBPATH"     : ['C:\\.conan\\123456\\1\\lib'],
        "BINPATH"     : ['C:\\.conan\\123456\\1\\lib'],
        "LIBS"        : [],
        "CPPDEFINES"  : [],
        "CXXFLAGS"    : [],
        "CCFLAGS"     : [],
        "SHLINKFLAGS" : [],
        "LINKFLAGS"   : [],
    },
    "boost_version" : "1.69.0"
}
Return('conan')

I have a scons /python file which needs CPPPATH,BINPATH,LIBPATH values as variable.我有一个 scons /python 文件,它需要 CPPPATH、BINPATH、LIBPATH 值作为变量。 I am trying to extract these values in following function in Sconscript :我试图在 Sconscript 的以下函数中提取这些值:

def getCPPPath():
          data = {'Return': lambda x: False}
            with open(file.txt, 'r') as f:
             exec(f.read(), data)
             return (data["conan"]["conan"]["CPPPATH"][0])
             print ("Path is:", ["conan"]["conan"]["CPPPATH"][0])

This gives me an error:这给了我一个错误:

scons: *** Return of non-existent variable ''conan''

How can I achieve this?我怎样才能做到这一点?

You can use the following code.您可以使用以下代码。 Note that exec is insecure as it runs all the code that is in your file.txt .请注意, exec是不安全的,因为它运行您的file.txt所有代码。 You also need to pass a dummy Return function into exec .您还需要将一个虚拟的Return函数传递给exec

data = {"Return": lambda x: False}

with open("file.txt", "r", encoding="utf-8") as f:
    exec(f.read(), data)

print(data['conan']['conan']['BINPATH'][0])
print(data['conan']['boost']['LIBPATH'][0])
print(data['conan']['conan']['CPPPATH'][0])

Prints印刷

['something']
['C:\\.conan\\123456\\1\\lib']
['something']

A lot easier and w/o using exec.使用 exec 更容易且无需使用。 You need to name your file conan.py (<= note the .py ending):您需要将文件conan.py (<= 注意 .py 结尾):

import conan

data = conan.conan
print(data['conan']['BINPATH'])

=> ['something']

Assuming you're trying to set up Conan with SCons, there's another way.假设您尝试使用 SCons 设置 Conan,还有另一种方法。 If this was "normal" Python, the two other answers would've been completely correct.如果这是“正常”的 Python,那么其他两个答案将是完全正确的。 However, since you're clearly using the SCons generator in Conan (otherwise, you wouldn't have ended up with that file), you have a file that's compatible with SCons.但是,由于您显然在 Conan 中使用了 SCons 生成器(否则,您将不会得到该文件),因此您有一个与 SCons 兼容的文件。 SCons naturally has additions on top of Python for managing its "own" files. SCons 自然地在 Python 之上添加了用于管理其“自己的”文件的附加功能。

Therefore, you don't need exec at all.因此,您根本不需要 exec 。 this is all you need:这就是你所需要的:

conan = env.SConscript("SConscript_conan")
# Note: you may need to change the path to SConscript_conan depending on your 
#       build system configuration. If you installed Conan in `build`, the
#       path should be "build/SConscript_conan"
env.MergeFlags(conan["conan"])

This also ensures you add all the other flags you need, not just the paths.这也确保您添加所需的所有其他标志,而不仅仅是路径。 Also saves you manual setup if you ever decide to add another library.如果您决定添加另一个库,还可以节省您的手动设置。

Return('conan') is an SCons function, which basically is equivalent to returning a variable (here: conan ) from an SConscript file into whatever executed it. Return('conan')是一个函数SCons的,这基本上等效于返回变量(这里: conan )从成任何执行它的一个SConscript文件。 Incorrect execution, or of course the lacking existence of a variable, is enough to cause the function to throw.不正确的执行,或者当然缺少变量的存在,足以导致函数抛出。 If you source the SConscript file properly, it should work as expected.如果您正确获取 SConscript 文件,它应该会按预期工作。

See also the Conan documentation , specifically the second code snippet, and the SCons docs on SConscript files另请参阅柯南文档,特别是第二个代码片段,以及SConscript 文件上的 SCons 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM