Created
April 8, 2014 13:14
-
-
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.
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
#! /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