Home > database >  How to save list, dict, int into file, and assign back from file
How to save list, dict, int into file, and assign back from file

Time:06-17

I have an PYQT5 UI with that allow user to store values into list, int, and qttextedit (allow user to write as code editor. Then i save existing values into txt file, so after user changed values can always come back and reload from the local file. The problem i face is when i assign back, it became a letter by letter, even the code assign back into UI will have \n. I also tried turn them into a list, but result still bad. So i think save into a txt is a bad idea, I looked for pickle but not sure if it can be used or how to use in this case.

score = 22
ocount = 88
scenario = {something here already}
...
usercode = 'var game = 0;\n if (game > 3){gid = 22}'

#List, int, dict, str store to txt file
with open(configName   ".txt", 'w') as f:
                f.write(repr(score)   "\n"   \
                    repr(ocount)   "\n"   \
                    repr(option)   "\n"   \
                    repr(option_ans)   "\n"   \
                    repr(matchname)   "\n"   \
                    repr(scenario)   "\n"   \
                    repr(_Options)   "\n"   \
                    repr(_Optionselected)   "\n"   \
                    repr(eInfo)   "\n"   \
                    repr(sVar)   "\n"   \
                    repr(cusotm)   "\n"   \
                    repr(survey1)   "\n"   \
                    repr(survey_ans)   "\n"   \
                    repr(eInfoName)   "\n"   \
                    repr(sVarName)   "\n"   \
                    repr(tempcode)   "\n"   \
                    repr(usercode))

The value output as below:

2
1
['q1_a1', 'q1_a2', 'q2_a1']
['q1_a1_ans', 'q1_a2_ans', 'q2_a1_ans']
['log1', 'log2']
{'log1': 'if (q1_a1) scores.team1 - 1q1_a1_ans== "23234"){\nproduct_ids = [11,22];\n}', 'log2': 'if (scores.team2 != 11\nproduct_ids = [33,2];\n}'}
['var q1_a1 = 11;', 'var q1_a2 = 11122;', 'var q2_a1 = 2;']
['var q1_a1_ans = 22;', 'var q1_a2_ans = 222;', 'var q2_a1_ans = 2;']
['123']
['321']
['cam1']
['q1_a1', 'q1_a2', 'q2_a1']
['q1_a1_ans', 'q1_a2_ans', 'q2_a1_ans']
['123']
['321']
'    \nelse if (\nelse if (\nelse if (\nelse if ('
'var has_answer = function (answer) {\n        return indexOf(answer) >= 0;\n    };'
#Load back

tmp=[]
f = open("lo.txt", "r")
Lines = f.readlines()
for x in Lines:
    tmp.append(x)
score = tmp[0]
ocount = tmp[1]
etc...

Result:


#try change to list
['[', "'", 'q', '1', '_', 'a', '1', "'", ',', ' ', "'", 'q', '1', '_', 'a', '2', "'", ',', ' ', "'", 'q', '2', '_', 'a', '1', "'", ']', '\n']

try add back to UI combobox

CodePudding user response:

use exec() to execute string as code

var = ['score', 'option', 'tempcode', 'usercode']
f = open("lo.txt", "r")
Lines = f.readlines()

for i in range(0, len(Lines)):
    exec(f'{var[i]} = {Lines[i]}')


    # check type
    exec(f'print({var[i]}, type({var[i]}))')

output

2 <class 'int'>
['q1_a1', 'q1_a2', 'q2_a1'] <class 'list'>
    
else if (
else if (
else if (
else if ( <class 'str'>
var has_answer = function (answer) {
        return indexOf(answer) >= 0;
    }; <class 'str'>

lo.txt

2
['q1_a1', 'q1_a2', 'q2_a1']
'    \nelse if (\nelse if (\nelse if (\nelse if ('
'var has_answer = function (answer) {\n        return indexOf(answer) >= 0;\n    };'

Or write them in json instead, make sure list is still list.

  • Related