I want to create simulator whitch adds JSON formated data to txt file until I will switch it off. Data should looks like that:
{user : [
{
"ID" : "12"
"button" : "red"
},
{
"ID" : "11"
"button" : "red"
}]
}
This is what i made:
import json
import random
import time
buttons = ['red', 'green', 'blue', 'yellow']
IDs = ['11', '12', '13', '14', '26', '51', '112', '3']
data = {}
data['user'] = []
data['user'].append({
'ID': random.choice(IDs),
'button': random.choice(buttons)
})
with open('data.txt', 'w') as outfile:
while True:
data = {}
data['user'] = []
data['user'].append({
'ID': random.choice(IDs),
'button': random.choice(buttons)
})
json.dump(data, outfile)
time.sleep(1)
First problem: data.txt file is empty when I stop program, second: data in JSON don't looks like I write before but looks like that:
{user : [
{
"ID" : "12"
"button" : "red"
}],
{user : [
{
"ID" : "11"
"button" : "red"
}]
}
I want just one tag user.
Thanks.
EDIT: Maybe It will be easier if I explain what it is for. Its simulation of voting, normally when voting starts people are pressing buttons with some options, there is ability to change mind and press diferent button then before, but when voting stops people can still press buttons and data in json file is updated but to data base are going only choices taken before voting stops.
I hope it will be easier to understand what I want to do. Maybe my concept is wrong and I should think about something different becouse it's too complicated.