Select to view content in your preferred language

script python doesn't run

2493
7
08-04-2017 08:06 AM
RobertoTantari
Emerging Contributor

hello,

I write the following script that it doesn't run

could you help me?

many thanks all

kind regards

Roberto

 

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

post the contents of the script rather than a screen grab

see Code Formatting... the basics++ on how to do this

JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
RobertoTantari
Emerging Contributor
>>> #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[0])
...         longitude = float(vals[1])
...         confid = int(vals[2])
...         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?

thanks

0 Kudos
RobertoTantari
Emerging Contributor

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"

many thanks

Roberto

0 Kudos
curtvprice
MVP Esteemed Contributor

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.

BrianWilson
Frequent Contributor

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))

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 blockIn 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)

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)

RobertoTantari
Emerging Contributor

that's very kind.

0 Kudos