import arcpy file = r"\\finch\gis\temp\kml\DowsonRansom.kml" f = open(file, 'r') dest = r'C:\Documents and Settings\jmkolb\Desktop\test.gdb\TEST' recordNumber = 0 for line in f: # Strips leading and trailing spaces from file lines r = line.strip() # Checks line integrity for < to start string, assigns variable t1 t1 = r.startswith("<") t2 = r.endswith(">") if line.find("<Placemark>") > 0: if recordNumber <> 0: record = [Description, Target, RowSpacing, StandCount, Name, CoordinateX, CoordinateY] #photolist] # Process: Create Table #arcpy.CreateTable_management(in_memory, "tbl", "'C:\\Documents and Settings\\jmkolb\\Desktop\\test.gdb\\TEST'", "") # Process: Append #arcpy.Append_management("record", dest, "NO_TEST", "Name \"Name\" true true false 255 Text 0 0 ,First,#,record[4],Name,-1,-1;FolderPath \"FolderPath\" true true false 255 Text 0 0 ,First,#;SymbolID \"SymbolID\" true true false 4 Long 0 0 ,First,#;AltitudeMode \"AltitudeMode\" true true false 2 Short 0 0 ,First,#;Snippet \"Snippet\" true true false 268435455 Text 0 0 ,First,#;PopupInfo \"PopupInfo\" true true false 268435455 Text 0 0 ,First,#;Description \"Description\" true true false 50 Text 0 0 ,First,#,record[0],Description,-1,-1;Target \"Target\" true true false 50 Text 0 0 ,First,#,record[1],Target,-1,-1;Row_Spacing \"Row_Spacing\" true true false 50 Text 0 0 ,First,#,record[2],Row_Spacing,-1,-1;Stand_Count \"Stand_Count\" true true false 50 Text 0 0 ,First,#,record[3],Stand_Count,-1,-1;Lat \"Lat\" true true false 8 Double 0 0 ,First,#,record[5],Lat,-1,-1;Long \"Long\" true true false 8 Double 0 0 ,record[6],Long,-1,-1", "") CoordinateX = "" CoordinateY = "" Name = "" Description = "" Target = "" RowSpacing = "" StandCount = "" photolist = [] recordNumber = recordNumber + 1 Attribute = "" Value = "" # Checks to see if the line starts and ends with Open and Closing Karrots if t1 == True and t2 == True: if line.find("<SimpleData name") > 0: # Looks for "name=", and pulls the entry between name=" and "><, and its corresponding value from the CDATA tag Attribute = line[line.find("name=") + 6: line.find("><")-1] Value = line[line.find("CDATA")+6:line.find("]]></SimpleData>")] if Attribute == "Description": Description = Value elif Attribute == "Target": Target = Value elif Attribute == "Row Spacing": RowSpacing = Value elif Attribute == "Stand Count": StandCount = Value elif Attribute == "PDFMaps_photos": photolist = line.split('"') # Sorts the list photolist.sort() # Reverses sort so Field Name is in position 0 photolist.reverse() # Returns list photolist = photolist[:photolist.index('PDFMaps_photos')] # Sorts list so Field Name is in position 0 photolist.sort() Attribute = photolist # Looks for Coordinate tag in the line if line.find("<coordinates>") > 0: # Grabs the value after <coordinates>, splitting by comma for a list CoordinateX = line[line.find("<coordinates>")+13:].split(",")[0] # Grabs the 2nd value in the list after finding <coordinates> CoordinateY = line[line.find("<coordinates>")+13:].split(",")[1] #print "X:",CoordinateX, "Y:",CoordinateY # Looks for <name> in the line if line.find("<name>") > 0: # Pulls the value between CDATA[ and ]]></name> Name = line[line.find("CDATA")+6:line.find("]]></name>")] #print "NAME",Name else: # Looks for <SimpleData name value within line that doesn't close with > if line.find("<SimpleData name") > 0: midline = " " # Looks in the open line to find the value between name= and ><, and prints it Attribute = line[line.find("name=") + 6: line.find("><")-1] # Finds the value for the attribute just found in above line multiLines = line[line.find("CDATA")+6:].strip() # Looks in the open line to find the close karrot elif line.find("]]></SimpleData>") > 0: multiLine = multiLines + midline.replace('\n'," ") + line[:line.find("]]></SimpleData>")] #Decision to use multiple line (2 or less) or multiLines (3 or more) for Description if len(multiLine) > len(multiLines): Description = multiLine else: Description = multiLines # For all lines that do not have an open or close karrot else: midline = midline + line f.close()
Solved! Go to Solution.