Created
April 14, 2019 17:08
-
-
Save dinnouti/d0adc443a4d19b6358d5d4d814293295 to your computer and use it in GitHub Desktop.
count the number of commas in a file and print if the number of commas is different than expected. useful for csv files where each row could have different number of commas breaking an import.
This file contains 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
# count the number of commas in a file and print if the number of commas is different than expected | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("filename", help="filename") | |
parser.add_argument("commas", help="number of expected commas", type=int) | |
args = parser.parse_args() | |
file = args.filename | |
number_commas = int(args.commas) | |
with open(file, 'r') as csv_file: | |
for line in csv_file: | |
lc = line.count(',') | |
if (lc != number_commas): | |
print(lc,' - ',line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment