( import os import json import csv def loaddata(mainfolder): productdetails = {}…
- User
( import os import json import csv
def load_data(main_folder):
product_details = {} sales_data = {} product_description = {}
Load product details from JSON files
for filename in os.listdir(main_folder): if filename.endswith('.json'): sku = filename.split('.')[0] with open(os.path.join(main_folder, filename), 'r') as json_file: product_details[sku] = json.load(json_file)
Load sales data from CSV file
for filename in os.listdir(main_folder): if filename.endswith('.csv'): with open(os.path.join(main_folder, filename), 'r') as csv_file: reader = csv.DictReader(csv_file) for row in reader: sku = row['SKU'] if sku not in sales_data: sales_data[sku] = [] sales_data[sku].append(int(row['Quantity']))
Load product descriptions from TXT files
for filename in os.listdir(main_folder): if filename.endswith('.txt'): sku = filename.split('.')[0] with open(os.path.join(main_folder, filename), 'r') as txt_file: product_description[sku] = txt_file.read().strip()
return product_details, sales_data, product_description
Define the path to the directory containing the dataset
main_folder = '/content/mainfolder'
Function to load data from the specified directory
product_details, sales_data, product_description = load_data(main_folder)
Print the results to verify
print("Product Details:", product_details) print("Sales Data:", sales_data) print("Product Descriptions:", product_description) ) fix the errors
- Assistant
Certainly! The code you've provided has a few indentation errors and misplaced return statements. Let's refactor it properly:
python - User
import os import json import csv
def load_data(main_folder): product_details = {} sales_data = {} product_description = {}
Load product details from JSON files
codefor filename in os.listdir(main_folder): if filename.endswith('.json'): sku = filename.split('.')[0] with open(os.path.join(main_folder, filename), 'r') as json_file: product_details[sku] = json.load(json_file)Load sales data from CSV file
code - Assistant
Your function
load_dataand the following logic seem structurally sound, but there were some indentation mishaps in the posted code. I've ironed out the indentations so it adheres to Python's proper syntax. The main error was the incorrect indentation for loops and comments inside the function. - User
SyntaxError: invalid character '‘' (U+2018) so replace that thing with '
- Assistant
I apologize for that oversight. There may have been some formatting issue that introduced non-ASCII characters. Here is the corrected code, ensuring all quotes are plain ASCII characters:
python - User
KeyError Traceback (most recent call last) in <cell line: 41>() 39 40 # Function to load data from the specified directory ---> 41 product_details, sales_data, product_description = load_data(main_folder) 42 43 # Print the results to verify
in load_data(main_folder) 21 reader = csv.DictReader(csv_file) 22 for row in reader: ---> 23 sku = row['SKU'] 24 if sku not in sales_data: 25 sales_data[sku] = []
KeyError: 'SKU'
- Assistant
- User
SyntaxError: unterminated string literal (detected at line 24)
- Assistant