TimeSeries01 / prepare.py
jashing
debug
0253298
import json
def prepare():
file_path = 'energy_train.jsonl.txt'
targets = []
# Open the JSON file and read it line by line
with open(file_path, 'r') as file:
for line in file:
#print (line)
try:
# Parse the JSON string in each line
data = json.loads(line)
# Extract the values of "timestamp" and "target" keys
#timestamp = data.get("timestamp")
target = data.get("target")
if target is not None:
#timestamps.append(timestamp)
targets.append(target)
except json.JSONDecodeError:
print(f"Skipping invalid JSON: {line}")
# Now, you have the extracted values in the "timestamps" and "targets" lists
# You can use these lists as needed
#print("Timestamps:", timestamps)
#print("Targets:", targets)
hist_size = 10
prd_size = 1
while targets:
# Get the first 5 elements and join them with spaces
history = ' '.join(map(str, targets[:hist_size]))
out = ' '.join(map(str,targets[hist_size:hist_size+prd_size]))
# Construct the JSON string
json_str = json.dumps({"color": out, "description": history})
print (json_str)
# Remove the first 6 elements from the float array
targets = targets[1:]
if len(targets) == hist_size:
break
if __name__ == "__main__":
#import fire
#fire.Fire(prepare)
prepare()