-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvparser.py
More file actions
107 lines (79 loc) · 2.91 KB
/
csvparser.py
File metadata and controls
107 lines (79 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
""" A class to handle CSV files. """
# Imports
import csv # CSV File Reading and Writing
import os # Miscellaneous operating system interfaces
# A class to handle CSV files
class CSVParser(object):
# Initialization
def __init__(self):
# Declare class variables
self.columncount = 0 # CSV file column count
self.filedata = [] # List for file data
self.message = "" # Error/success message
self.rowcount = 0 # CSV file row count
self.success = False # Successful file open
# A method to load CSV file
def load_file(self, file, fieldseparator=",", textdelimiter='"'):
# Set the file opened to false
self.success = False
# Check if file path is None
if not file:
self.message = "Error: no file."
return False
# Check if file path is not empty
if file == "":
self.message = "Error: filename is empty."
return False
# Extension check
ext = file.lower()
if not ext.endswith(".csv"):
self.message = "Error: invalid file extension."
return False
# Check if file exists
if not os.path.exists(file):
self.message = "Error: file does not exist."
return False
# Check if path is an existing regular file
if not os.path.isfile(file):
self.message = "Error: not a file."
return False
# Try to load CSV file
try:
# Clear list
self.filedata = []
# Create a file handle and open file using csv.reader
filehandle = open(file, "r")
csvfile = csv.reader(filehandle, delimiter=fieldseparator,
quotechar=textdelimiter)
# Count the number of columns
self.columncount = len(next(csvfile))
# Return position to zero
filehandle.seek(0)
# Loop lines and append them into a list
for line in csvfile:
listline = []
for i in range(self.columncount):
listline.append(line[i])
self.filedata.append(listline)
del(listline)
# Count the number of rows
self.rowcount = sum(1 for line in self.filedata)
# Discard variables
del(csvfile)
# Close file handle
if filehandle:
filehandle.close()
# Successful file open
self.success = True
except IOError as e:
self.message = "I/O error({0}): {1}".format(e.errno, e.strerror)
except:
self.message = "Error: unable to open file."
finally:
if self.success:
self.message = "File opened successfully."
return True
else:
return False