Skip to content

Instantly share code, notes, and snippets.

@paulsena
Created April 8, 2014 13:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsena/10122271 to your computer and use it in GitHub Desktop.
Save paulsena/10122271 to your computer and use it in GitHub Desktop.
Python script to check for mismatched number of Columns in a CSV file. Useful for checking files before loading into ServiceNow because SN doesn't tell you what line number had a mismatch, it just rejects the whole file.
#! /usr/bin/env python
import fileinput
import sys
headerColCnt = 0
mismatches = 0
delimiter = "|"
def process(line):
global headerColCnt
global mismatches
if fileinput.lineno() == 1:
headerColCnt = line.count(delimiter)
print ("Columns in Header: ", headerColCnt)
else:
if line.count(delimiter) != headerColCnt:
print ("Mismatched Column # on line: ", fileinput.lineno())
mismatches += 1
for line in fileinput.input(openhook=fileinput.hook_encoded("utf8")):
process(line)
print ( "Lines processed.", fileinput.lineno() )
print ( "Mismatches found.", mismatches )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment