Best way to retrieve variable values from a text file?

后端 未结 10 2175
再見小時候
再見小時候 2020-11-30 19:44

Referring on this question, I have a similar -but not the same- problem..

On my way, I\'ll have some text file, structured like:

var_a: \'home\'
var_         


        
10条回答
  •  攒了一身酷
    2020-11-30 20:16

    hbn's answer won't work out of the box if the file to load is in a subdirectory or is named with dashes.

    In such a case you may consider this alternative :

    exec open(myconfig.py).read()
    

    Or the simpler but deprecated in python3 :

    execfile(myconfig.py)
    

    I guess Stephan202's warning applies to both options, though, and maybe the loop on lines is safer.

提交回复
热议问题