-1

So I have this kind of data received:

[{
    'Status': 0,
    'Button': False,
    'Message': None,
    'Id': None,
    'hu': 0,
    'Mode': 'LocModePresence',
    'mac': '00011171815E',
    'mapId': '17_1_0',
    'Seq': 236,
    'tam': False,
    'temperature': 0.0,
    'time': 1603797352911,
    'type': 'TTT',
    'x': 2716.0,
    'y': 648.0,
    'zone': '301990146'
}, {
    'Status': 0,
    'Button': False,
    'Message': '6e0002000c00',
    'Id': '3_2',
    'hu': 0,
    'Mode': 'LocModePresence',
    'mac': '00011171815E',
    'mapId': '17_1_0',
    'Seq': 237,
    'tam': False,
    'temperature': 0.0,
    'time': 1603797357105,
    'type': 'TTT',
    'x': 2716.0,
    'y': 648.0,
    'zone': '301990146'
}]

And I want to write it into JSON file:

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(my_data, f, ensure_ascii=False, indent=4)

If i want to take this data that have as string (for debug reason) why when I put it inside 2 ' sign this show me error:

illegal target for variable annotation

I want to be able to write this on my disk and also read it to compare to another file that I will get (and also this file need to be write on the disk)

falukky
  • 1,099
  • 2
  • 14
  • 34
  • so I don't understand clearly what the problem is , I copied pasted your code and I was able to save it a file succesfully, – Saif Asif Oct 27 '20 at 11:58

1 Answers1

-1

When you read from the file you should use read().

The different between read() readline() and readline() are documented here:

When should I ever use file.read() or file.readlines()?

import json

my_data = [
    {
        'Status': 0,
        'Button': False,
        'Message': None,
        'Id': None,
        'hu': 0,
        'Mode': 'LocModePresence',
        'mac': '00011171815E',
        'mapId': '17_1_0',
        'Seq': 236,
        'tam': False,
        'temperature': 0.0,
        'time': 1603797352911,
        'type': 'TTT',
        'x': 2716.0,
        'y': 648.0,
        'zone': '301990146',
    },
    {
        'Status': 0,
        'Button': False,
        'Message': '6e0002000c00',
        'Id': '3_2',
        'hu': 0,
        'Mode': 'LocModePresence',
        'mac': '00011171815E',
        'mapId': '17_1_0',
        'Seq': 237,
        'tam': False,
        'temperature': 0.0,
        'time': 1603797357105,
        'type': 'TTT',
        'x': 2716.0,
        'y': 648.0,
        'zone': '301990146',
    },
]


if __name__ == '__main__':
    with open('data.json', 'w', encoding='utf-8') as _file:
        json.dump(my_data, _file, ensure_ascii=False, indent=4)

    with open('data.json', 'r', encoding='utf-8') as _file:
        str_content = _file.read()

    print(type(str_content))
    json_content = json.loads(str_content)
    print(type(json_content))
Alon Barad
  • 1,491
  • 1
  • 13
  • 26
  • Ok this works but when i try to load this saved file (json.loads) i have this error: raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) – falukky Oct 27 '20 at 12:04
  • the idea of using `json.dumps` is to store result in variable as I explained in the comments and be able to use that variable (incl. to write it to a file). Also you should use `write()`, not `writelines()` – buran Oct 27 '20 at 12:09
  • So with your example: after i have this file on my disk and another one, how can i compare they both ? – falukky Oct 27 '20 at 12:12