jashing/TimeSeries01
019
1import json2 3 4def prepare():5 file_path = 'energy_train.jsonl.txt'6 targets = []7 8 # Open the JSON file and read it line by line9 with open(file_path, 'r') as file:10 for line in file:11 #print (line)12 try:13 # Parse the JSON string in each line14 data = json.loads(line)15 16 # Extract the values of "timestamp" and "target" keys17 #timestamp = data.get("timestamp")18 target = data.get("target")19 20 if target is not None:21 #timestamps.append(timestamp)22 targets.append(target)23 24 except json.JSONDecodeError:25 print(f"Skipping invalid JSON: {line}")26 27 # Now, you have the extracted values in the "timestamps" and "targets" lists28 # You can use these lists as needed29 #print("Timestamps:", timestamps)30 #print("Targets:", targets)31 32 hist_size = 1033 prd_size = 134 35 while targets:36 # Get the first 5 elements and join them with spaces37 history = ' '.join(map(str, targets[:hist_size]))38 out = ' '.join(map(str,targets[hist_size:hist_size+prd_size]))39 40 # Construct the JSON string41 json_str = json.dumps({"color": out, "description": history})42 43 print (json_str)44 # Remove the first 6 elements from the float array45 targets = targets[1:]46 if len(targets) == hist_size:47 break48 49 50if __name__ == "__main__":51 #import fire52 #fire.Fire(prepare)53 prepare()54 55 