import struct
filepath = 'hits.dat'
with open(filepath, 'rb') as fp:
while True:
bytes = fp.read(28)
if not bytes or len(bytes) != 28:
break
event_id, track_id, x, y, z = struct.unpack(">HHddd", bytes)event_id: 256
track_id: 0
x: -7.942855253805373e-275
y: 6.303619193466582e-17
z: 8.500503648212859e-45events = {
'event_id': 1,
'tracks' : [{
'track_id': 1,
'coordinates': [{
'x':1,
'y':2,
'z':3
},
{
'x':4,
'y':5,
'z':6
}]
}]
}import struct
import json
filepath = 'hits.dat'
events = {}
with open(filepath, 'rb') as fp:
buffer = fp.read(28)
while len(buffer) == 28:
event_id, track_id, x, y, z = struct.unpack(">HHddd", buffer)
buffer = fp.read(28)
event = events.setdefault(event_id, dict(event_id=event_id, tracks={}))
track = event['tracks'].setdefault(track_id, dict(coordinates=[]))
track['coordinates'].append(dict(x=x, y=y, z=z))
with open('hits.json', 'w') as fp:
json.dump(events, fp, indent=2)events = {
1: {
'event_id': 1,
'tracks': {
1: {
'track_id': 1,
'coordinates': [
{'x': 1, 'y': 2, 'z': 3},
{'x': 4, 'y': 5, 'z': 6}
]
},
2: {
'track_id': 2,
'coordinates': [
{'x': 12, 'y': 22, 'z': 33},
{'x': 44, 'y': 55, 'z': 66}
]
}
}
}
} Но в задаче не сказано, что отдельные треки одного событий идут подряд и отдельные координаты одного трека тоже идут подряд.
{
"256": {
"event_id": 256,
"tracks": {
"0": {
"coordinates": [
{
"x": -7.942855253805373e-275,
"y": 6.303619193466582e-17,
"z": 8.500503648212859e-45
}
]
}
}
}
}while len(bytes) == 28:. Верна ли такая запись?---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-8-222c575c6e2a> in <module>
7 buffer = fp.read(28)
8 while len(bytes) == 28:
----> 9 event_id, track_id, x, y, z = struct.unpack(">HHddd", buffer)
10 buffer = fp.read(28)
11
error: unpack requires a buffer of 28 bytesbytes на buffer, то код работает без ошибок.Уточните задачу и можно будет оптимизировать решение по памяти перейдя к потоковой выдаче.
Если изменить bytes на buffer, то код работает без ошибок.
Мне необходимо будет сделать визуализацию треков по координатам для каждого эвента.


Вы как-то умудрились его собрать и сохранить, а значит оперативки вам хватит (наверно своп помог), однако даже одно чтение будет занимать чудовищное время.
import json
EOL = '\n'
def convert_big_map2values_jsons(fn):
with open(fn) as f:
big_data = json.load(f)
idx = {}
p = 0
with open(fn + 's', 'w') as f:
for k, v in big_data.items():
idx[k] = p
line = json.dumps(v)
writed_size = f.write(line + EOL)
p += writed_size
with open(fn + '.index.json', 'w') as f:
json.dump(idx, f)
def load_by_index(index, filename: str):
with open(filename + '.index.json', 'r') as f:
idx = json.load(f)
offset = idx[str(index)]
with open(filename + 's', 'r') as f:
f.seek(offset)
return json.loads(f.readline())
if __name__ == '__main__':
fn = 'big_fucking_file.json'
fn = 'tmp/1.json'
convert_big_map2values_jsons(fn)
print('Loaded by index:', load_by_index(256, fn))
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-9-4a987f01cad0> in <module>
1 fn = '_hits_test.json' #big_fucking_file.json
2 fn = 'tmp/1.json'
----> 3 convert_big_map2values_jsons(fn)
4 print('Loaded by index:', load_by_index(256, fn))
<ipython-input-4-a4397750b172> in convert_big_map2values_jsons(fn)
6 def convert_big_map2values_jsons(fn):
7 with open(fn) as f:
----> 8 big_data = json.load(f)
9
10 idx = {}
/opt/anaconda3/envs/myenv2/lib/python3.7/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
--> 296 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
297
298
/opt/anaconda3/envs/myenv2/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 parse_int is None and parse_float is None and
347 parse_constant is None and object_pairs_hook is None and not kw):
--> 348 return _default_decoder.decode(s)
349 if cls is None:
350 cls = JSONDecoder
/opt/anaconda3/envs/myenv2/lib/python3.7/json/decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
/opt/anaconda3/envs/myenv2/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)fn = 'big_fucking_file.json'
fn = 'tmp/1.json'---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-12-7388d3c65ab7> in <module>
2 fn2 = 'tmp/first_json'
3 convert_big_map2values_jsons(fn1)
----> 4 print('Loaded by index:', load_by_index(256, fn2))
<ipython-input-4-a4397750b172> in load_by_index(index, filename)
24 def load_by_index(index, filename: str):
25 with open(filename + '.index.json', 'r') as f:
---> 26 idx = json.load(f)
27
28 offset = idx[str(index)]
/opt/anaconda3/envs/myenv2/lib/python3.7/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
--> 296 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
297
298
/opt/anaconda3/envs/myenv2/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 parse_int is None and parse_float is None and
347 parse_constant is None and object_pairs_hook is None and not kw):
--> 348 return _default_decoder.decode(s)
349 if cls is None:
350 cls = JSONDecoder
/opt/anaconda3/envs/myenv2/lib/python3.7/json/decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
/opt/anaconda3/envs/myenv2/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Допустимо два раза fn?