Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ColumnFileFactory: # Create factory class
- def melt(self, headers, rows, seperators=[" ", "\n"], blank=False): # Create function to create a file by melting data
- if type(blank) == str: # If blank is a string
- for row in rows: # Loop through rows
- data_index = 0 # Create index counter of data
- for data in row: # Loop through data in row
- if type(data) == int: # If type of data is int
- if data == 0: # If data is 0
- row[data_index] = blank if type(blank) == str else "" # Replace it with blank
- data_index += 1 # Count up data index counter
- if len(rows) < 1: # If there isnt any rows
- return "Failed to create formatted file." # Return error
- column = list(map(lambda row : 0, rows[0])) # Create some empty columns
- rows = [headers, []] + rows # Add headers to rows
- pr_rows = [] # Create a list for print rows
- for row in rows: # Loop through all the rows
- pr_row = [] # Create an empty list for the new print row
- data_index = 0 # Current iteration count
- for data in row: # Loop through data in row
- data = str(data) # Cast data to string
- if len(column) > data_index: # Check if there is access for a column
- if len(data) > column[data_index]: # If length of data is more than the column padding length
- column[data_index] = len(data) # Set the column padding length to the data length
- pr_row.append(data) # Put the new data into a print row
- data_index += 1 # Count up for iteration
- pr_rows.append(pr_row) # Add the print row to the print rows
- new_data = [] # Create an empty array for the new data
- for row in pr_rows: # For every row in print rows
- data_index = 0 # Set data interation counter to 0
- data_row = [] # Create an empty array for new row
- for data in row:
- data = str(data) # Cast data to string
- data_row.append(data.ljust(column[data_index])) # Rewrite the row but using spacing
- data_index += 1 # Count up for iteration
- new_data.append(str(seperators[0]).join(data_row)) # Add the row as print format with padding as given seperators
- return str(seperators[1]).join(new_data) # Return the mixture
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement