Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Read the file and store the data in a list of dictionaries
- def read_data(file_name):
- with open(file_name, 'r') as f:
- headers = f.readline().strip().split()
- data = []
- for line in f:
- row = line.strip().split()
- data.append({header: value for header, value in zip(headers, row)})
- return data
- # Define a function to sort the data
- def sort_data(data, column, order):
- if order == 'asc':
- return sorted(data, key=lambda x: x[column])
- elif order == 'desc':
- return sorted(data, key=lambda x: x[column], reverse=True)
- else:
- print("Invalid order. Please choose 'asc' or 'desc'.")
- # Get user input for file name, column, and order
- file_name = input("Enter the file name (including .txt extension): ")
- column = input("Choose a column to sort (NIM, NAMA, NILAI): ")
- order = input("Choose an order (asc, desc): ")
- # Read the data from the file
- data = read_data(file_name)
- # Sort and print the data
- sorted_data = sort_data(data, column, order)
- for row in sorted_data:
- print("{:<10} {:<15} {:<5}".format(row['NIM'], row['NAMA'], row['NILAI']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement