I write the following script that it doesn't run
could you help me?
many thanks all
post the contents of the script rather than a screen grab
see Code Formatting... the basics++ on how to do this
The error message is fairly clear, you have a parsing or indentation issue on line 8. Go back and double check your spacing, and posting the code as Dan suggests would also help people comment on specific issues, like indentation.
>>> #Script to Import data to a feature class within a geodatabase... import arcpy, os... try:... arcpy.env.workspace = "C:/ArcpyBook/Ch7data/Wildfires/WildlandFires.mdb"... f = open("C:/ArcpyBook/Ch7data/Wildfires/NorthAmericaWildfires_2007275.txt","r")... lstFires = f.readlines()... cur = arcpy.InsertCursor("FireIncidents")... cntr = 1... for fire in lstFires:... if 'Latitude' in fire:... continue... vals = fire.split(",")... latitude = float(vals)... longitude = float(vals)... confid = int(vals)... pnt = arcpy.Point(longitude, latitude)... feat = cur.newRow()... feat.shape = pnt... feat.setValue("CONFIDENCEVALUE", confid)... cur.insertRow(feat)... arcpy.AddMessage("Record number" + str(cntr) + "written to feature class")... cntr = cntr + 1... except:... print arcpy.GetMessages()... finally:... del cur... f.close() ... ... ...
that's ok now?
damn i' haven't save the .py file (1° screen shot)
as soon as possible i rewrite the whole code on python window
and posted it according to "script site rule"
Getting indents perfect in Python can be a real pain. If you dont' use an IDE (like IDLE or PythonWin) I suggest using a nice text editor like Notepad++ to convert tabs to spaces, match parentheses, etc. I use Wing and have found it worth the money but there are many free options too.
I have general comments for you that I think will really help you.
(1) Make the code inside your try-except block(s) as short as possible
If you feel you need to put a lot of code into try blocks then use more than one try block!
The reason is that it is really hard to see what broke - there is just some problem somewhere in the block
Usually it's better to have the script abort because you coded wrong (for example an uninitialized variable) than to have it jump into an except block, hiding the actual problem. (You get a generic ESRI error message instead of a syntax error)
In your example for instance I would use a short block at the top
fc = "FireIncidents"try: cur = arcpy.InsertCursor(fc)except Exception as e: print("Could not get a cursor for %s, error was: %s" % (file, e))
fc = "FireIncidents"
cur = arcpy.InsertCursor(fc)except Exception as e: print("Could not get a cursor for %s, error was: %s" % (file, e))
You can use more than one try except block, and don't put code inside blocks that you think is perfect - it's better to have some stupid typo stop your code than to have it hide inside a try-except block. In my example I put fc = "FireIncidents" outside the block.
(2) Catch the actual error message that comes back and show it
try: a = d["my key"]except Exception as e: print ("I got an exception", e)
a = d["my key"]
except Exception as e:
print ("I got an exception", e)
If you run this you get
('I got an exception', NameError("name 'd' is not defined",))
This is much more useful, now I know I forgot to define my dictionary "d" or I typed its name wrong.
And of course you could push the messages into ArcPy with something like this
arcpy.AddMessage("I got an exception: %s" % e)
that's very kind.
Retrieving data ...