简体   繁体   中英

Excel to JSON using Python, how do I format this data to my needs?

So I want to read an excel file and extract the data into a JSON file using python.

The excel data is formatted as so:

Header 1  |   Header 2   | Header 3

   x00           x01          x02
   x10           x11          x12
    .             .            .
    .             .            .

Now I've managed to get most of the coding done right I think which is the following. However I really need to get the json output in a very specific format, which is why I used the line for data[i]

import json
import pandas as pd
  
df = pd.read_excel (r'C:\Users\ezammit\Documents\Python Scripts\FILE.xlsx', sheet_name='sheet_1')

#initialize data
data=[0 for i in range(len(df) - 1)]

for i in range(len(df) - 1):    
    
        data[i] = r'{"'+str(df.columns.values[0])+'": "' +str(df.loc[i][0])+'", '+str(df.columns.values[1])+'": "' +str(df.loc[i][1])+'", '+str(df.columns.values[2])+'": "' +str(df.loc[i][2])+'"}' 

with open('Savedwork.json', 'w') as json_file: 
    json.dump(data, json_file)

As I mentioned, I really want to get a specific format in the JSON file which should be exactly as the following:

{"Header1":"data[0][0]", "Header2":"data[0][1]", "Header3":"data[0][2]"},
{"Header1":"data[1][0]", "Header2":"data[1][1]", "Header3":"data[1][2]"},
{"Header1":"data[2][0]", "Header2":"data[2][1]", "Header3":"data[2][2]"},
...

Any help would be appreciated

Instead of "creating" JSON yourself, you can create a python dictionary and then let Python convert it to JSON String and store it in a file.

The first error was when you passed len(df)-1 to range function. The range function automatically goes till passedValue-1 so you had to pass it just len(df) .

And inside the loop just create a dict instead of a String. Here is the modified code for you:

import json
import pandas as pd
  
df = pd.read_excel (r'D:\example.xlsx', sheet_name='Sheet1')

#initialize data
data=[0 for i in range(len(df))]


for i in range(len(df)):    
        # data[i] = r'{"'+str(df.columns.values[0])+'": "' +str(df.loc[i][0])+'", '+str(df.columns.values[1])+'": "' +str(df.loc[i][1])+'", '+str(df.columns.values[2])+'": "' +str(df.loc[i][2])+'"}' 
        data[i] = {str(df.columns.values[0]) : str(df.loc[i][0]), str(df.columns.values[1]):  str(df.loc[i][1]), str(df.columns.values[2]): str(df.loc[i][2])}
output_lines = [json.dumps(line)+",\n" for line in data]
output_lines[-1] =   output_lines[-1][:-2] # remove ",\n" from last line
with open('Savedwork.json', 'w') as json_file:
        json_file.writelines(output_lines)

Here is the link to sample Excel File, I used: Sample XLSX

And here is the sample output of the code:

{"Header1": "1", "Header2": "2", "Header3": "3"},
{"Header1": "6", "Header2": "5", "Header3": "4"},
{"Header1": "7", "Header2": "8", "Header3": "9"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM