Skip to content

Instantly share code, notes, and snippets.

@dinnouti
Created April 14, 2019 17:08
Show Gist options
  • Save dinnouti/d0adc443a4d19b6358d5d4d814293295 to your computer and use it in GitHub Desktop.
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.
# 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