Hi everyone. This is my first post here.
I am zero in python. I am trying to create script by myself and I thought chatgpt would help but I failed. Here's what I'm trying to do.
I have 23 values each for productcode, assetcode, and name neatly arranged in an excel file. How do I generate 23 json files?
I want to generate these types of json file for every product code. So eventually I will have 23 json files.
FTJ_productCode_assetcode_name
So e.g.
Every json file should go into its own individual folder. The name of the json file should be same as the "name", i.e. Oak Veneer 01.json in this example.
I copy pasted this code by getting help from chatgpt and made a .py file.
I am getting this error
I am attaching the a zip file containing folders, the python file and the excel file
I am zero in python. I am trying to create script by myself and I thought chatgpt would help but I failed. Here's what I'm trying to do.
I have 23 values each for productcode, assetcode, and name neatly arranged in an excel file. How do I generate 23 json files?
I want to generate these types of json file for every product code. So eventually I will have 23 json files.
Output:{
"version": 1,
"type": "material",
"productCode": "MC012",
"assetcode": "A019",
"name": "Oak Veneer 01",
"tags": "",
"res": ""
}
Every folder name is in the following format. FTJ_productCode_assetcode_name
So e.g.
FTJ_MC012_A019_OakVeneer01
Every json file should go into its own individual folder. The name of the json file should be same as the "name", i.e. Oak Veneer 01.json in this example.
I copy pasted this code by getting help from chatgpt and made a .py file.
import json import os import pandas as pd # Read data from Excel file excel_file = "mywork.xlsx" excel_data = pd.read_excel(excel_file) # Loop through the data and create JSON files and folders for index, entry in excel_data.iterrows(): # Create folder name folder_name = f"FTJ_{entry['productCode']}_{entry['assetcode']}_{entry['name'].replace(' ', '')}" os.makedirs(folder_name, exist_ok=True) # Create folder if it doesn't exist # Create JSON content json_content = { "version": 1, "type": "material", "productCode": entry["productCode"], "assetcode": entry["assetcode"], "name": entry["name"], "tags": "", "res": "" } # Create 23 JSON files with different names for i in range(1, 24): json_file_path = os.path.join(folder_name, f"{entry['name'].replace(' ', '')}{i}.json") # Write JSON content to file with open(json_file_path, "w") as json_file: json.dump(json_content, json_file, indent=4) print(f"Generated JSON file: {json_file_path}")I am getting the following error.
I am getting this error
Error:Traceback (most recent call last):
File "C:\Python312\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
return self._engine.get_loc(casted_key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc
File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'productCode'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\freshaa\COOLTEXTAGEN.py", line 12, in <module>
folder_name = f"FTJ_{entry['productCode']}_{entry['assetcode']}_{entry['name'].replace(' ', '')}"
~~~~~^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\pandas\core\series.py", line 1121, in __getitem__
return self._get_value(key)
^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\pandas\core\series.py", line 1237, in _get_value
loc = self.index.get_loc(label)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc
raise KeyError(key) from err
KeyError: 'productCode'
What should I do to fix this? I checked multiple times the column title of productCode in my excel file but it is totally fine. I am attaching the a zip file containing folders, the python file and the excel file
Attached Files