Untitled
# -*- coding: utf-8 -*- """ Created on Mon Jan 6 15:03:26 2025 @author: louis """ import csv # Show the menu function definition def showMenu(): print("=" * 40) print("Please choose option") print("A - Load and display the data from csv file.") print("B - Display the maximum value of selected sensor data.") print("Q - Exit the program.") print("=" * 40) # Load CSV data function def loadCSVData(fname): datalist = [] with open(fname) as openfile: csvreader = csv.reader(openfile) next(csvreader) # Skip the header row for row in csvreader: print(row) datalist.append(row) return datalist # Function to display maximum sensor data def dispMax(alldata): cdata = [] print("Please choose option") print("1) Temperature Sensor") print("2) Pressure Sensor") print("3) Vibration Sensor") cindex = int(input("Enter 1, 2 or 3: ")) - 1 for row in alldata: cdata.append(float(row[cindex + 1])) maxdata = max(cdata) print(f"Maximum data is {maxdata:.2f}") # Main program def main(): while True: showMenu() choice = input("Please select A, B, or Q: ").strip().lower() if choice == 'a': fname = input("Please enter name of csv file (file.csv): ").strip() try: alldata = loadCSVData(fname) except FileNotFoundError: print("File not found. Please try again.") elif choice == 'b': try: if 'alldata' in locals(): dispMax(alldata) else: print("No data loaded. Please load data first.") except ValueError: print("Invalid data. Please check the CSV file.") elif choice == 'q': print("Exiting the program.") break else: print("Sorry, invalid selection. Try again!") if __name__ == "__main__": main()
Leave a Comment