Converting an excel file to a specific Json in python using openpyxl library

2024/11/16 23:45:12

enter image description here enter image description here

I have the Excel data with the format shown in the image preview. How can I convert it into a JSON using Python?

Expected Output:

file_name = [ { A: Measurement( time=10, X1=1, X2=4 ), B: Measurement( time=10, X1=1.2, X2=4.1 ), C: Measurement( time=10, X1=1, X2=4 ) }, { A: Measurement( time=11, X1=1.1, X2=4 ), B: Measurement( time=11, X1=1, X2=4.2 ), C: Measurement( time=11, X1=1, X2=4 ) }, { A: Measurement( time=12, X1=1.3, X2=4.1 ), B: Measurement( time=12, X1=1, X2=4.1 ), C: Measurement( time=12, X1=1.2, X2=3.9 ) } ]

Answer

The desired output can be achieved using nested dictionary objects.

import openpyxl
import json
workbook = openpyxl.load_workbook(r'd:/temp/data.xlsx')
sheet = workbook.active
data_dict = {}
# skipping header
for row in sheet.iter_rows(min_row=2, values_only=True):device_id, time, X1, X2 = rowhour = time.hour  # Extract hour from the timeif hour not in data_dict:data_dict[hour] = {}data_dict[hour][device_id] = (hour, X1, X2)
# dictionary to JSON
json_data = []
for hour, measurements in data_dict.items():json_data.append({device_id: f"Measurement( time={hour}, X1={X1}, X2={X2} )" for device_id, (hour, X1, X2) in measurements.items()})
file_name = json.dumps(json_data, indent=2)    
print(file_name)

Output

[                                                                                             {                                                                                           "A": "Measurement( time=10, X1=1, X2=4 )",                                                "B": "Measurement( time=10, X1=1.2, X2=4.1 )",                                            "C": "Measurement( time=10, X1=1, X2=4 )"                                                 },                                                                                          {                                                                                           "A": "Measurement( time=11, X1=1, X2=4 )",                                                "B": "Measurement( time=11, X1=1.2, X2=4.1 )",                                            "C": "Measurement( time=11, X1=1, X2=4 )"                                                 }                                                                                           
] 

Update

Revised the code for new requirement in comment.

import json
import pandas as pd
from datetime import datetime
data = pd.read_excel(r'd:\temp\data.xlsx')
data['time_str'] = data['time'].apply(lambda t: t.strftime("%Y, %#m, %#d, %H, %M"))
grouped = data.groupby('time_str').apply(lambda g: g.to_dict(orient='records')).tolist()
output = []
for measurements in grouped:mea_dict = {}for meamnt in measurements:device_id = meamnt['Device_id']x1 = meamnt['X1']x2 = meamnt['X2']time_str = meamnt['time_str']mea_str = f"Measurement( calculated_date=datetime({time_str}), X1={x1}, X2={x2} )"mea_dict[device_id] = mea_stroutput.append(mea_dict)
fname = json.dumps(output, indent=2)
print(fname)

Output

[                                                                                             {                                                                                           "A": "Measurement( calculated_date=datetime(2023, 1, 1, 10, 00), X1=1.0, X2=4.0 )",       "B": "Measurement( calculated_date=datetime(2023, 1, 1, 10, 00), X1=1.2, X2=4.1 )",       "C": "Measurement( calculated_date=datetime(2023, 1, 1, 10, 00), X1=1.0, X2=4.0 )"        },                                                                                          {                                                                                           "A": "Measurement( calculated_date=datetime(2023, 2, 1, 10, 10), X1=1.0, X2=4.0 )",       "B": "Measurement( calculated_date=datetime(2023, 2, 1, 10, 10), X1=1.2, X2=4.1 )",       "C": "Measurement( calculated_date=datetime(2023, 2, 1, 10, 10), X1=1.0, X2=4.0 )"        }                                                                                           
]        
https://en.xdnf.cn/q/118872.html

Related Q&A

Why this algorithm can sort data in descending order

I study python programming and try to sort data in descending order.#sort1 below is successfully sorted but I cannot understand why this happen. Also, data[i], data[data.index(mn)] = data[data.index(m…

Processing.py - Unknown Error on Class Definition

I have no idea how to fix this error. Maybe theres an open parenthesis or quotation mark somewhere before this line?What is wrong with this code?Class Ribbon: # I got an error on this line! def __ini…

How can I implement a stopwatch in my socket Python program

I am creating a very simple ping program in Python that will send 16- 64 bytes of information to a local server, once the server has received all the bytes, it will send back a 1 Byte message back to t…

Making a quiz with shuffled questions

I am wanting to join the Royal Air Force and thought as a good way to prepare I should code myself a quiz about their aircraft.There are 28 aircraft that I have added to the quiz. For example - Where i…

How to create a timer that resets for quiz games?

I trying to create a timer for my quiz game. It should reset after every right question. But problem with my code is that it keeps increasing speed after every time it resets. timeCount = 30 def countd…

Stucking of python multiprocessing.pool

i have multiprocessing script with "pool.imap_unordered". I ran into a problem when the script got stuck but i check CPU usage — nothing happening (by "top" command on ubuntu). Goi…

python: merge two csv files

I have a problem while Im doing my assignment with python. Im new to python so I am a complete beginner.Question: How can I merge two files below?s555555,7 s333333,10 s666666,9 s111111,10 s999999,9and…

Generate a custom formated string with python

I have a Javascript code that generates a string (similar to uuid) stringHere it is the js code: var t = "xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxx", i = (new Date).getTime(); return e = t.repla…

PySide-6.6: clicked signal sends extra boolean argument to slot

I just upgraded PySide6 (PySide6==6.6.0 from 6.2.2) and a new behavior is wreaking havoc with my GUI program. Every place where I have a clicked signal, the hooked up slot is receiving an extra bool==F…

SMTPConnectError when using Django

Im using django-registration for handling of users registration. I tried to signup in order to test it, after testing it, I got this errorSMTPConnectError at /accounts/register/Being trying to find a s…