Best way to retrieve variable values from a text file?

后端 未结 10 2179
再見小時候
再見小時候 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:09

    Suppose that you have a file Called "test.txt" with:

    a=1.251
    b=2.65415
    c=3.54
    d=549.5645
    e=4684.65489
    

    And you want to find a variable (a,b,c,d or e):

    ffile=open('test.txt','r').read()
    
    variable=raw_input('Wich is the variable you are looking for?\n')
    
    ini=ffile.find(variable)+(len(variable)+1)
    rest=ffile[ini:]
    search_enter=rest.find('\n')
    number=float(rest[:search_enter])
    
    print "value:",number
    

提交回复
热议问题