Select to view content in your preferred language

Problem with arcpy.MakeXYEventLayer in Python Script

1830
7
Jump to solution
04-25-2018 09:17 AM
JasonBartling
Emerging Contributor

I'm working on a project to add point locations to a map using the arcpy.MakeXYEventLayer_managment tool in Python.  This tool is acting unpredictable and causing a fatal EOFError (End Of File) exception every other time I run my script.  Half the time it completes normally with no modifications.  I've tried different inputs to the MakeXYEventLayer tool consisting of a .csv file, .dbf file, and a table view.  None of these seem to make a difference.  This is a current short snippet of the latest attempt:

 

import arcpy

basePath = 'C:\Users\Jason\Documents\SchoolStuff\GEO 4393C'
workspacePath = basePath+'\FinalProject\Project.gdb'

arcpy.env.workspace = workspacePath
arcpy.env.overwriteOutput = True

locationsFilePath = basePath+'\FinalProject\Locations\MiniGolfLocations.xls'
locationsTable = 'MiniGolfLocations'

arcpy.ExcelToTable_conversion(locationsFilePath,locationsTable,'MiniGolfLocations')
print 'Done converting spreadsheet to table'

arcpy.MakeXYEventLayer_management(locationsTable,'LONGITUDE','LATITUDE','USpoints')
print 'Done creating xy event layer'

arcpy.CopyFeatures_management('USpoints',workspacePath+'\USpoints')

print 'Done creating USpoints'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

Has anyone else run into this problem before or have any suggestions? 

0 Kudos
1 Solution

Accepted Solutions
forestknutsen1
MVP Regular Contributor

You could try using an InsertCursor—Data Access module | ArcGIS Desktop 

It is way faster and more stable then using an events layer in my experience. 

This is how I like to do paths. As in Dan and Darren links point out.

import os

path1 = r"c:\stuff"
path2 = r"more\and\more"

full_path = os.path.join(path1, path2)

View solution in original post

7 Replies
DarrenWiens2
MVP Honored Contributor

Whether or not this is your problem, you're playing with fire using backslashes in file paths (e.g. "\t" = tab). In short, don't use backslashes, unless in a raw string. See here for three options - don't use the fourth option.

 

JasonBartling
Emerging Contributor

I tried using double backslashes, raw strings with backslashes, forward slashes, and raw strings with forward slashes.  None of those options changed my execution results.

0 Kudos
DanPatterson_Retired
MVP Emeritus

and for visual examples of Darren's choices on the survey...

Filenames and file paths in python

is a good place to get the reasons why

forestknutsen1
MVP Regular Contributor

You could try using an InsertCursor—Data Access module | ArcGIS Desktop 

It is way faster and more stable then using an events layer in my experience. 

This is how I like to do paths. As in Dan and Darren links point out.

import os

path1 = r"c:\stuff"
path2 = r"more\and\more"

full_path = os.path.join(path1, path2)
JasonBartling
Emerging Contributor

Using an InsertCursor instead looks to have fixed my problem.  It's quite a few more lines of code in setup of the feature class and a SearchCursor, but at least it works consistently.  Thank you for this suggestion.

0 Kudos
DanPatterson_Retired
MVP Emeritus

In a very short period of time you will have to move to python 3, so be aware of future pitfalls regarding path constructs since there are more pitfalls ahead...

basePath = 'C:\Users\Jason\Documents\SchoolStuff\GEO 4393C'
workspacePath = basePath+'\FinalProject\Project.gdb'

  File "<ipython-input-1-bc7a010eb6d9>", line 1
    basePath = 'C:\Users\Jason\Documents\SchoolStuff\GEO 4393C'
              ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

So you remember the 'pirate' thing and raw encode the first bit then try to use that subsequently

basePath = r'C:\Users\Jason\Documents\SchoolStuff\GEO 4393C'  # ---- cleverly 'r'r'r'ed
workspacePath = basePath+'\FinalProject\Project.gdb'

# ---- big check ---- looks good ---
workspacePath
Out[3]: 'C:\\Users\\Jason\\Documents\\SchoolStuff\\GEO 4393C\\FinalProject\\Project.gdb'

# ---- on to append in Copy_management ----

workspacePath+'\USpoints'
  File "<ipython-input-4-393953a8a67e>", line 1
    workspacePath+'\USpoints'
                 ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape

# ---- it's back again

And unless you are trapping errors you will get nice silent failures or unrelated error messages.

Python 3 opens a whole new world called Unicode when it comes to working with text.

0 Kudos
JasonBartling
Emerging Contributor

Ok.  Thanks for the information.

0 Kudos