简体   繁体   English

将 Excel 文件转换为 JSON 并排除一列

[英]Convert Excel File to JSON and Exclude a Column

For example, I have created an Excel file named “records.xlsx”例如,我创建了一个名为“records.xlsx”的 Excel 文件

My Excel Data我的 Excel 数据

Car Name   Price   Count
Honda       100     1
Tata        150     10
GMR         200      5

And I am importing it like this我是这样导入的

import excel2json

excel2json.convert_from_file('records.xlsx')

Which is converting my excel file to following JSON:将我的 excel 文件转换为以下 JSON:

[
    {
        "Car Name": "Honda",
        "Price": "100",
        "Count": "1"
    },
    {
        "Car Name": "Tata",
        "Price": "150",
        "Count": "10"
    },
    {
        "Car Name": "GMR",
        "Price": "150",
        "Count": "5"
    }
] 

I don't want the Price column in my resulting JSON.我不希望生成的 JSON 中出现Price列。 How can I convert this Excel file to JSON directly such that only columns Car Name and Count will appear in resulting JSON?如何直接将此 Excel 文件转换为 JSON,以便只有列Car NameCount会出现在生成的 JSON 中?

The library that you are using doesn't seem to provide option to exclude column when loading it.您正在使用似乎没有提供在加载列时排除列的选项。 I think the easiest thing for you to do would be to filter the unwanted column after you load the JSON.我认为对您来说最简单的事情是在加载 JSON 后过滤不需要的列。

your_json = excel2json.convert_from_file('records.xlsx')
for object in your_json:
    del object['Price']

You will be left with your_json without the Price entries.您将得到没有Price条目的your_json

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

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