List has invalid syntax error

5749
8
Jump to solution
01-07-2021 01:38 PM
CliveSwan
Occasional Contributor II

Greetings,

I am getting an error from a list from a csv that I want to use to loop through features in SDE.

It was working, but is throwing an error now??

 

 

envarp_list = ["AirQualityReceptors_Ply", "AirQualityReceptors_Pt", "LandscapeCharacterAreas_Ply", "Viewpoints_Pt","LV_ZTV_Ply", "AssessmentLocationsAll_Pt"]

 

 

 

envarp_list = ["AirQualityReceptors_Ply", "AirQualityReceptors_Pt", "LandscapeCharacterAreas_Ply", "Viewpoints_Pt","LV_ZTV_Ply", "AssessmentLocationsAll_Pt"]
 
print("LOOP")
fcs = arcpy.ListFeatureClasses("server.gis.*")
for fc in fcs:
    for envarp in arcpror1_list:
        if fc == envarp:
            print(fc)
               print(new_name)
               arcpy.FeatureClassToFeatureClass_conversion(fc, outLocation, new_name)
        else:
            print("NOT in list")
            

print("END")

 

 

The list is much longer, it would be good to read directly from the csv file.

 

Error message is:

 

SyntaxError                               Traceback (most recent call last)
File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py, in parse:
Line 35:    return compile(source, filename, mode, PyCF_ONLY_AST)

SyntaxError: invalid syntax (<string>, line 29)

 

 

This was working earlier with a smaller list, really need to read it from the csv, without looping 4 times

 

Any pointers appreciated.

 

Clive

 

1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

envarp_list

is only used once in the code where is

arcpror1_list


... sort of retired...

View solution in original post

8 Replies
DanPatterson
MVP Esteemed Contributor

envarp_list

is only used once in the code where is

arcpror1_list


... sort of retired...
BlakeTerhune
MVP Regular Contributor

I see you do have an extra indent in the two lines under print(fc)

DanPatterson
MVP Esteemed Contributor

That should have flagged a syntax error as well.  I wonder if there is no linting and code checking going on.  Where is this being run?

The python thing in Pro?

A separate python IDE?


... sort of retired...
CliveSwan
Occasional Contributor II

It is running but not returning the csv??

 

f = open("W:\My Documents\user\Priority List 1.csv")

envarp_list = csv.reader(f)
               
print("LOOP")
fcs = arcpy.ListFeatureClasses("server.gis.*")
for fc in fcs:
    new_name = fc.split(".")[-1]
    for envarp in envarp_list:
        if fc == envarp:
            print(fc)
            print(new_name)
            arcpy.FeatureClassToFeatureClass_conversion(fc, outLocation, new_name)
        else:
            print("NOT in list")
            

print("END")

 

Just returns not in the file. There are thousands of Features and I want to export a subset to gdb??? 

DanPatterson
MVP Esteemed Contributor

@CliveSwan I can't even get past your first line

 

f = "W:\My Documents\user\Priority List 1.csv"
  File "<ipython-input-3-dc6c4d32386f>", line 1
    f = "W:\My Documents\user\Priority List 1.csv"
       ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 15-16: truncated \uXXXX escape

 

Are you using a python IDE like Spyder, pycharm, whatever ???

Syntax errors and code syntax issues aren't being flagged for you.

You need to raw encode your paths....  r"W:\My Documents\user\Priority List 1.csv"  ... a little r goes a long way

 

AND

Throw multiple print statements into your code

If you are getting strings/text from files, like you are, you would be advised to use "strip" to remove leading trailing spaces...

oops = "TestMe          "

test_against = ["TestMe", " TestMe", "TestMe "]
oops in test_against
False

[oops == i.strip() for i in test_against]
[False, False, False]

[oops.strip() == i.strip() for i in test_against]  # ****** bingo!!!
[True, True, True]

... sort of retired...
CliveSwan
Occasional Contributor II

I am using Pycharm. That happens when spend 4 hours working on a problem. 

Not throwing any errors, doesn't seem to read the csv?? It didn't return a single Features.

 

 

DanPatterson
MVP Esteemed Contributor

check the path... put in print statements to see if it is returning anything like

envarp_list = csv.reader(f)
for i in envarp_list:
    print(i)

then when you know there is stuff in there check for the leading trailing spaced AND is the case the same? (upper,  lower etc )


... sort of retired...
RachelGomez
New Contributor II

Syntax errors
Make sure you are not using a Python keyword for a variable name.
Check that you have a colon at the end of the header of every compound statement, including for, while, if, and def statements.
Check that indentation is consistent. 
Make sure that any strings in the code have matching quotation marks.

 

Regards,

Rachel Gomez

0 Kudos