-
Notifications
You must be signed in to change notification settings - Fork 1
/
NCSS-TroubleshootTextFiles_ImportProcess.py
146 lines (105 loc) · 4.6 KB
/
NCSS-TroubleshootTextFiles_ImportProcess.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#-------------------------------------------------------------------------------
# Name: NCSS-TroubleshootTextFiles_ImportProcess
# Purpose: This script was used to troubleshoot the lab_chemical_properties
# and lab_method_code text file during the import process.
#
# Author: Adolfo.Diaz
#
# Created: 30/12/2019
# Copyright: (c) Adolfo.Diaz 2019
# Licence: <your licence>
#-------------------------------------------------------------------------------
## ===================================================================================
def errorMsg():
try:
exc_type, exc_value, exc_traceback = sys.exc_info()
print "################### Error Message ###########"
theMsg = "\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[1] + "\n\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[-1]
#AddMsgAndPrint(theMsg,2)
print theMsg
except:
#AddMsgAndPrint("Unhandled error in errorMsg method", 2)
print "Unhandled error in errorMsg method"
pass
import arcpy
import csv, sys, traceback, codecs
if __name__ == '__main__':
txtPath = r'N:\flex\Dylan\NCSS_Characterization_Database\Updated_Schema_2019\NCSS_LabData_export\lab_chemical_properties.txt'
pedonFGDB = r'N:\flex\Dylan\NCSS_Characterization_Database\Updated_Schema_2019\Test.gdb\lab_chemical_properties'
arcpy.env.workspace = pedonFGDB
# Put all the field names in a list; used to initiate insertCursor object
fieldList = arcpy.Describe(pedonFGDB).fields
nameOfFields = []
fldLengths = list()
for field in fieldList:
if field.type != "OID":
nameOfFields.append(field.name)
if field.type.lower() == "string":
fldLengths.append(field.length)
else:
fldLengths.append(0)
# The csv file might contain very huge fields, therefore increase the field_size_limit:
# Exception thrown with IL177 in legend.txt. Not sure why, only 1 record was present
try:
csv.field_size_limit(sys.maxsize)
except:
csv.field_size_limit(sys.maxint)
# Initiate Cursor to add rows
numOfRowsAdded = 0
cursor = arcpy.da.InsertCursor(pedonFGDB,nameOfFields)
## reload(sys) # Reload does the trick!
## sys.setdefaultencoding('ISO-8859-1')
# counter for number of records successfully added; used for reporting
reader = csv.reader(open(txtPath, 'rb'), delimiter='|', quotechar='"')
#reader = csv.reader(open(txtPath, 'rb'), delimiter='|')
#reader = csv.reader(codecs.open(txtPath, 'rb', encoding='utf-8', errors='strict'), delimiter='|', quotechar='"')
lineNumber=0
# Return a reader object which will iterate over lines in txtPath
for rowInFile in reader:
try:
currentLine = rowInFile
lineNumber+=1
# Skip first record if text files contain headers
if lineNumber==1:
#print rowInFile; exit()
continue
#print 1
newRow = list() # list containing the values that make up a specific row
fldNo = 0 # list position to reference the field lengths in order to compare
#print 2
for value in rowInFile:
value = value.decode('ISO8859-1')\
if value == '' or value == 'NULL':
value = None
if value != None and value.startswith(" "):
value = value[1:len(value)]
newRow.append(value)
#newRow.append(value.decode('ISO8859-1'))
#print "Inserting line #: " + str(lineNumber) + "\n"
#try:
cursor.insertRow(newRow)
#print "Inserted line #: " + str(lineNumber) + "\n"
#print 5
numOfRowsAdded += 1
# print ""
#except:
# pass
#del newRow, rowInFile
#print 6
del currentLine, newRow,fldNo
except:
#pass
print "Line Error: " + str(lineNumber)
errorMsg()
## exc_type, exc_value, exc_traceback = sys.exc_info()
## print "################### Error Message ###########"
## theMsg = "\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[1] + "\n\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[-1]
## print theMsg
## #if theMsg.find('codec') > -1:
## #print "Codec problem"
## print currentLine
## print lineNumber
## print "\n\n"
## continue
print "Total Lines: " + str(lineNumber)
del cursor, reader