-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (33 loc) · 1.26 KB
/
main.py
File metadata and controls
44 lines (33 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import chardet
from json import loads, dumps
def detect_file_encoding(file_path):
""" Detects the encoding of a file """
with open(file_path, 'rb') as file:
raw_data = file.read()
result = chardet.detect(raw_data)
return result['encoding']
def read_fyt_file(file_path, encoding):
with open(file_path, 'rb') as file:
bytes = file.read()
initial_bytes = bytes[0: 26]
final_byte = bytes[-1:]
data = bytes[26:-1]
decoded_json = loads(data.decode(encoding))
return decoded_json, initial_bytes, final_byte
def write_fyt_file(file_path, encoding, data, initial_bytes, final_byte):
with open(file_path, 'wb') as file:
file.write(initial_bytes)
json_data = dumps(data, indent=2)
json_data = json_data.replace('\n', '\r\n')
file.write(json_data.encode(encoding))
file.write(final_byte)
# Example usage
#encoding = detect_file_encoding('temp_v2.fyt')
encoding = 'Windows-1252'
print(encoding)
data, initial_bytes, final_byte = read_fyt_file('temp_v2.fyt', encoding)
for part in data['sections'][0]['parts']:
if part['group-name'] == 'Humidity':
part['phases'][0]['value'] = 50
write_fyt_file('temp2b.fyt', encoding, data, initial_bytes, final_byte)
pass