Skip to content

Instantly share code, notes, and snippets.

@Segerberg
Last active March 7, 2019 07:47
Show Gist options
  • Save Segerberg/0846b1e3855fee96e810e1840c3a5dc7 to your computer and use it in GitHub Desktop.
Save Segerberg/0846b1e3855fee96e810e1840c3a5dc7 to your computer and use it in GitHub Desktop.
import csv
delimiter = ','
delimiter_number = 3
import logging
errors = 0
logging.basicConfig(filename='file.log', level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
with open('deniro.csv', 'r') as csvfile:
for row in csvfile:
# Check for non valid ISO 8859-1 characters
try:
bytes(row, "ISO 8859-1")
except UnicodeEncodeError:
logging.error('Illegal ISO 8859-1 character for post %s', row)
errors += 1
continue
# Check that csv ends with CRLF
if not row.endswith('\n'):
logging.error('Missing CRLF/LF for post %s', row)
errors += 1
csvfile.seek(0)
reader = csv.reader(csvfile, delimiter=delimiter, quotechar='"')
for row in reader:
print (len(row))
if len(row) != delimiter_number:
errors += 1
logging.error('Wrong delimiter for post %s', row)
if errors > 0:
print ('{} errors found in csv, please see logfile'.format(errors))
else:
logging.info('Csv file passed all checks')
print ('Csv file passed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment