-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfile_reader.py
More file actions
70 lines (52 loc) · 2.73 KB
/
file_reader.py
File metadata and controls
70 lines (52 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# file_reader.py
import csv
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def read_structure_parameters(csv_file):
# Initialize a dictionary to store unique values for each header
param_dict = {}
# Try-except block to handle file reading exceptions
try:
# Open the CSV file and create a reader object
with open(csv_file, mode='r', encoding='utf-8') as file:
reader = csv.reader(file)
headers = next(reader, None) # First row as headers
if not headers:
raise ValueError("CSV file is empty or headers are missing")
num_columns = len(headers)
param_dict = {header: [] for header in headers}
for row in reader:
if not any(row): # Skip empty rows
continue
# Truncate or extend the row to match the header length
row = row[:num_columns] + [''] * (num_columns - len(row))
for header, value in zip(headers, row):
if value and value not in param_dict[header]:
param_dict[header].append(value)
# Log the successful reading of the file
# logging.info(f"Structure parameters read successfully from {csv_file}")
print(f"Structure parameters read successfully from {csv_file}")
except Exception as e:
# Log any exceptions that occur during file reading
logging.error(f"An error occurred while reading the structure parameters from {csv_file}: {e}")
# Return the dictionary of parameters
return param_dict
def read_scene_descriptions(file_path):
# Initialize a list to store scene descriptions
descriptions = []
# Try-except block to handle file reading exceptions
try:
# Open the text file and read lines
with open(file_path, 'r', encoding='utf-8') as file:
# Read the first line as the title, removing commas and extra whitespace
title = next(file).replace(',', '').strip()
# Read the rest of the lines, stripping whitespace, removing commas, and ignoring empty lines
descriptions = [line.replace(',', '').strip() for line in file if line.strip()]
# Log the successful reading of the file
logging.info(f"Scene descriptions read successfully from {file_path}")
except Exception as e:
# Log any exceptions that occur during file reading
logging.error(f"An error occurred while reading scene descriptions from {file_path}: {e}")
# Return the list of descriptions
return {title: descriptions}