简体   繁体   English

Excel 到 JSON 使用 Python,我如何根据需要格式化此数据?

[英]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.所以我想读取一个 excel 文件并使用 python 将数据提取到 JSON 文件中。

The excel data is formatted as so: excel 数据的格式如下:

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]但是我真的需要以非常特定的格式获取 json output,这就是为什么我使用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:正如我提到的,我真的很想在 JSON 文件中获取特定格式,它应该完全如下所示:

{"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.您可以创建一个 python 字典,然后让 Python 将其转换为 JSON String 并将其存储在一个文件中,而不是您自己“创建”JSON。

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) .第一个错误是当您将len(df)-1传递到range function 时。范围 function 自动转到passedValue-1 ,因此您必须只传递它len(df)

And inside the loop just create a dict instead of a String.在循环内部只创建一个dict而不是一个字符串。 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这是示例 Excel 文件的链接,我使用了: Sample XLSX

And here is the sample output of the code:这是代码的示例 output:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将实时JSON数据(从传感器)格式化为Python中的Excel工作簿? - How do I format real time JSON data (from sensors) into an Excel Workbook in Python? 如何创建一个将在python中以JSON格式序列化的数据结构? - How do I create a data structure that will be serialized this JSON format in python? 我想用“%”号显示我的EXCEL数据。 我如何使用python做到这一点 - I want to display my EXCEL data with “%” sign. How can i do so by using python 如何在Python中使用Elasticsearch索引JSON格式的文件? - How do I index JSON format files using Elasticsearch in Python? 尝试以 json 格式对我的数据进行分析。 到目前为止,我的代码下面我的问题是如何加入我的所有数据,请帮助我是 Python 新手 - trying do analysis to my data in json format. my code below so far my questions is how can I join all my data , pls help I am new to python 如何使用 python 将 json 对象传输到 excel 文件中? - How do I transfer json objects into an excel file using python? 如何使用 json.dumps() 格式化我的 Python 字典以匹配以下 JSON 格式 - How can I format my Python dictionary to match the below JSON format using json.dumps() 如何将我的元组转换为格式,以便 Python 中的 JSON 格式可以接受 - How do I convert my tuple into the format so that it is acceptable for the JSON format in Python 如何使用 python 将修改后的数据添加到我的 json 文件的顶部? - How do I add modified data to the top of my json file using python? 如何使用 Python 自动将数据写入 Excel 文件? - How do I write data into an Excel file automatically using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM