0

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.

Reyaven
  • 29
  • 3
  • Do you really want to add elements to file one by one? If you want the output you need, you have to create the whole object (one big dictionary) in memory and then write it to disk. – Ilya V. Schurov Feb 04 '17 at 12:01

1 Answers1

1

data in JSON don't looks like I write before

Thats because your are creating a new dictionary every time in your while loop. You have to first build the complete dictionary and then write it to your file.

Also your while loop has no exit condition. It will run forever.

First problem: data.txt file is empty when I stop program

This is because python will not directly write it to the actual file but into a buffer. See this question for more information. How come a file doesn't get written until I stop the program?

JSON is not the best choice for continuously appending data to a file, especially if you want it to be correct JSON in between appending. You will have to write the whole dict every time you update it. Use CSV for that instead.

Community
  • 1
  • 1
trixn
  • 15,761
  • 2
  • 38
  • 55
  • ok but I want to save this data and have access whenever I want, If I build complete dict I have access to this data after json.dump() command. First problem solved, thanks :) – Reyaven Feb 04 '17 at 12:15
  • what do you mean with "have access"? You do have access to your dict. So you can do whatever you want with it. – trixn Feb 04 '17 at 12:19
  • Hmm, I will read about CSV. What I meant: Someone else have to parse this file and get ID and pressed button, this is only simulation of pressing buttons by people, Its happend in some range of time and for example: when person pressed button 'red' and then he changed his mind and pressed 'blue' I have to notice that. I hope this is a little bit better explained. – Reyaven Feb 04 '17 at 12:35
  • Okay but a file on the hard drive may not be the easiest way to exchange data. How will "the other one" get access to the file ? Where is the other one? – trixn Feb 04 '17 at 12:46
  • Second person working with PHP+MySQL and parsing this file then updating data in tables whitch looks like: [ID: button_pressed], this file is still updated and when this "other one" program stops reading it this one should still adding data there. – Reyaven Feb 04 '17 at 12:57