forked from ncss-tech/NASIS-Pedons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create_CSV_files_from_PedonDatabase.py
122 lines (91 loc) · 4.51 KB
/
Create_CSV_files_from_PedonDatabase.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
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: Adolfo.Diaz
#
# Created: 22/08/2017
# Copyright: (c) Adolfo.Diaz 2017
# Licence: <your licence>
#-------------------------------------------------------------------------------
## ===================================================================================
def AddMsgAndPrint(msg, severity=0):
# prints message to screen if run as a python script
# Adds tool message to the geoprocessor
#
#Split the message on \n first, so that if it's multiple lines, a GPMessage will be added for each line
try:
print msg
#for string in msg.split('\n'):
#Add a geoprocessing message (in case this is run as a tool)
if severity == 0:
arcpy.AddMessage(msg)
elif severity == 1:
arcpy.AddWarning(msg)
elif severity == 2:
arcpy.AddError("\n" + msg)
except:
pass
## ===================================================================================
def errorMsg():
try:
exc_type, exc_value, exc_traceback = sys.exc_info()
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)
except:
AddMsgAndPrint("Unhandled error in errorMsg method", 2)
pass
## ================================================================================================================
def splitThousands(someNumber):
""" will determine where to put a thousands seperator if one is needed.
Input is an integer. Integer with or without thousands seperator is returned."""
try:
return re.sub(r'(\d{3})(?=\d)', r'\1,', str(someNumber)[::-1])[::-1]
except:
errorMsg()
return someNumber
## ===================================================================================
def convertTablesToCSV(GDB):
try:
env.workspace = GDB
tablesToConvert = [table for table in arcpy.ListTables()]
for fc in arcpy.ListFeatureClasses():
tablesToConvert.append(fc)
if len(tablesToConvert):
for table in tablesToConvert:
#if not table == 'phtext':continue
numOfRows = int(arcpy.GetCount_management(table).getOutput(0))
fieldNames = [f.name for f in arcpy.ListFields(table)]
tempTable = table + "Temp.csv"
permTable = outputFolder + os.sep + table + ".txt"
if arcpy.Exists(outputFolder + os.sep + tempTable):
arcpy.Delete_management(outputFolder + os.sep + tempTable)
if arcpy.Exists(permTable):
continue
#arcpy.Delete_management(permTable)
AddMsgAndPrint("Converting " + table + " to CSV file. Num of records: " + splitThousands(numOfRows))
arcpy.TableToTable_conversion(table,outputFolder,tempTable)
# file needs to be opened in Universal mode otherwise an error is thrown:
# new-line character seen in unquoted field - do you need to open the file in universal-newline mode
with open(outputFolder + os.sep + tempTable,'rU') as csvFile:
#csvReader = csv.reader(open(csvFile, 'rU'), dialect=csv.excel_tab)
csvReader = csv.reader(csvFile)
with open(permTable,"wb") as csvResult:
csvWrite = csv.writer(csvResult,delimiter='|',quotechar='"')
for row in csvReader:
csvWrite.writerow(row[1:])
arcpy.Delete_management(outputFolder + os.sep + tempTable)
except:
errorMsg()
AddMsgAndPrint("\nUnable to get table information from: " + GDB,2)
exit()
# =========================================== Main Body ==========================================
# Import modules
import sys, string, os, traceback, re, arcpy,csv
from arcpy import env
if __name__ == '__main__':
inputGDB = arcpy.GetParameter(0)
outputFolder = arcpy.GetParameterAsText(1)
inputGDB = r'E:\All_Pedons\NCSS_Characterization_Database\NCSS_Lab_Data_Mart_05152017.gdb'
outputFolder = r'E:\All_Pedons\NCSS_Characterization_Database\CSV_files_new'
convertTablesToCSV(inputGDB)