Created
July 14, 2023 17:51
-
-
Save NicerNewerCar/fe87df0997c07e85b53fd259696f983f to your computer and use it in GitHub Desktop.
Removes trailing commas from file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This script will remove trailing commas from a CSV file, if they exist. | |
| #@Author: Anthony Lombardi | |
| #@Date: 07/14/2023 | |
| import os, sys | |
| if __name__ == "__main__": | |
| # Grab the file name from the command line | |
| if len(sys.argv) != 2: | |
| print("Usage: python remove-trailing-commas.py <file_name>") | |
| sys.exit(1) | |
| file_name = sys.argv[1] | |
| if not os.path.isfile(file_name): | |
| print("System cannot file the file: " + file_name) | |
| sys.exit(1) | |
| # Read in the file | |
| with open(file_name, 'r') as f: | |
| lines = f.readlines() | |
| # Remove trailing commas | |
| new_lines = [] | |
| for line in lines: | |
| if line[-2] == ',': | |
| new_lines.append(line[:-2] + '\n') | |
| else: | |
| new_lines.append(line) | |
| # Write the new file | |
| with open(file_name, 'w') as f: | |
| for line in new_lines: | |
| f.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment