-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_and_calculate_sum.py
69 lines (54 loc) · 3.2 KB
/
search_and_calculate_sum.py
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
def search_and_calculate_sum():
# Ask the user for keywords
keywords = input("Enter keywords (separated by commas): ").lower().split(',')
# Ask for the file name to search
file_name = input("Enter the file name to search: ")
# Ask for the new file name to create
new_file_name = input("Enter the new file name to create: ")
# Open the existing file for reading with error handling for encoding
try:
with open(file_name, 'r', encoding='utf-8') as old_file:
# Open a new file for writing
with open(new_file_name, 'w') as new_file:
# Initialize a variable to store the sum of the 7th column
column_7_sum = 0
# Iterate through each line in the old file
for line in old_file:
# Check if any keyword is present in the line (case-insensitive)
if any(keyword.strip().lower() in line.lower() for keyword in keywords):
# Write the line to the new file
new_file.write(line)
# Split the line into columns using space as the delimiter
columns = line.split()
# Try to convert the 7th column to a float and add it to the sum
try:
column_7_sum += round(float(columns[2]),2)
except (ValueError, IndexError):
print("Warning: Unable to extract or convert the 7th column to a number.")
print(f"Lines containing the keywords are saved in {new_file_name}.")
print(f"Sum of the 3rd column: {round(column_7_sum, 2)}")
except UnicodeDecodeError:
print("Error: Unable to decode the file with 'utf-8' encoding. Trying 'latin-1' encoding.")
# Retry opening the file with 'latin-1' encoding
with open(file_name, 'r', encoding='latin-1') as old_file:
# Open a new file for writing
with open(new_file_name, 'w') as new_file:
# Initialize a variable to store the sum of the 7th column
column_7_sum = 0
# Iterate through each line in the old file
for line in old_file:
# Check if any keyword is present in the line (case-insensitive)
if any(keyword.strip().lower() in line.lower() for keyword in keywords):
# Write the line to the new file
new_file.write(line)
# Split the line into columns using space as the delimiter
columns = line.split()
# Try to convert the 7th column to a float and add it to the sum
try:
column_7_sum += round(float(columns[2]),2)
except (ValueError, IndexError):
print("Warning: Unable to extract or convert the 7th column to a number.")
print(f"Lines containing the keywords are saved in {new_file_name}.")
print(f"Sum of the 3rd column: {round(column_7_sum, 2)}")
# Call the function to run the program
search_and_calculate_sum()