automatyczny_kelner/dataset/datasetConverter.py

54 lines
1.6 KiB
Python
Raw Normal View History

2023-05-26 01:29:28 +02:00
import csv
def convert_dataset(input_file, output_file):
attributes = {
1: ["empty", "low", "medium", "high"],
2: ["empty", "full"],
3: ["none", "available"],
4: ["none", "available"],
5: ["unavailable", "available"],
6: ["low", "medium", "high"],
7: ["no", "yes"],
8: ["no", "yes"]
}
# Create a mapping dictionary for attribute values
mapping = {}
for attr, values in attributes.items():
mapping[attr] = {value: index for index, value in enumerate(values)}
converted_dataset = []
# Read the input CSV file
with open(input_file, "r") as csvfile:
reader = csv.reader(csvfile)
header = next(reader) # Skip the header row
# Convert the data rows
for row in reader:
converted_row = []
for i, value in enumerate(row):
# Convert the attribute values
if i + 1 in mapping:
converted_value = mapping[i + 1][value]
else:
converted_value = value
converted_row.append(converted_value)
converted_dataset.append(converted_row)
# Write the converted dataset to a new CSV file
with open(output_file, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
# Write the header row
#writer.writerow(header)
# Write the converted data rows
for row in converted_dataset:
writer.writerow(row)
# Example usage:
convert_dataset("dataset/Dataset.csv", "dataset/converted_dataset.csv")