Using STR function in Arcpy Statement

2192
10
Jump to solution
01-31-2019 03:03 PM
Brian_McLeer
Occasional Contributor II

I have created an SDE database in which I have copied feature classes from my production database from State Plane into WGS 84 for web mapping applications. The final step is to create views, and I am trying to automate the process as I have hundreds of feature classes.

Below, I am trying to make a list of all the feature classes and use the name of the feature class. The middle parameter starts with a prefix of v_ then grabs the feature class name (ie: v_watermeters). The third variable is the SQL statement used to create the view definition, where it grabs all the fields (SELECT * FROM ) then I want to use the feature class name (ie: SELECT * FROM watermeters). 

I am having trouble with the third variable and concatenating the string values to make valid code.

We are currently on ArcGIS 10.3.1 and SQL Server 2012.

import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server Database sde.sde"
featureclasses = arcpy.ListFeatureClasses()

for fc in featureclasses:
 arcpy.arcpy.CreateDatabaseView_management(
 fc, os.path.join("Database Connections\\Server Database sde.sde","v_"+str(featureclasses), "select * from "+featureclasses)
Brian
0 Kudos
1 Solution

Accepted Solutions
FrédéricPRALLY
Esri Contributor

Hi,

Try following code :

import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server Database sde.sde"
featureclasses = arcpy.ListFeatureClasses()

for fc in featureclasses:
 arcpy.arcpy.CreateDatabaseView_management(
 "Database Connections\\Server Database sde.sde", "v_" + str(fc.split('.')[2]), "select * from " + fc)

View solution in original post

10 Replies
DarrenWiens2
MVP Honored Contributor

Consider the following, and note that the individual item is 'fc' and the list, which never changes, is 'featureclasses':

featureclasses = ['a', 'b', 'c']

for fc in featureclasses:
    print('start')
    print(fc, featureclasses)
    print('end')

start
a ['a', 'b', 'c']
end
start
b ['a', 'b', 'c']
end
start
c ['a', 'b', 'c']
end
0 Kudos
JoeBorgione
MVP Emeritus

In line 8, you refer twice to 'featureclasses' which is the name of the list you are iterating through.  I'm not sure why you are doing that.  It seems to me you would want the value of fc instead, the current item of the list.  

Personally, I prefer to use the format() function when I can to concatenate characters and variable values.

for fc in featureclasses:
 arcpy.arcpy.CreateDatabaseView_management(
 fc, os.path.join("Database Connections\\Server Database sde.sde",'v_{}'.format(fc), 'select * from {}'.format(fc)
That should just about do it....
Brian_McLeer
Occasional Contributor II

I updated line 6 and 7 with your code but seems to still report an error:

Traceback (most recent call last):
File "C:\path\to\python\file.py", line 8, in <module>
arcpy.arcpy.CreateDatabaseView_management(fc, os.path.join("Database Connections\\Server DB sde.sde",'v_{}'.format(fc), 'select * from {}'.format(fc)))
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 4069, in CreateDatabaseView
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000837: The workspace is not the correct workspace type.
ERROR 000735: View Definition: Value is required
Failed to execute (CreateDatabaseView).

import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server DB sde.sde"
featureclasses = arcpy.ListFeatureClasses()
 
for fc in featureclasses:
 arcpy.arcpy.CreateDatabaseView_management(fc, os.path.join("Database Connections\\Server DB sde.sde",'v_{}'.format(fc), 'select * from {}'.format(fc)))‍‍‍‍‍‍‍
Brian
0 Kudos
DarrenWiens2
MVP Honored Contributor

Check your parameters against the help.

Create Database View—Help | ArcGIS for Desktop 

The tool takes 3 parameters.

1.) database

2.) view name

3.) view definition

Also, am I seeing double or do I see "arcpy.arcpy"? That is a red flag.

DanPatterson_Retired
MVP Emeritus
# ---- what you have
featureclasses = ['a', 'b', 'c']

'v_' + str(featureclasses)

"v_['a', 'b', 'c']"

# ---- what you want, one at a time

fc = featureclasses[0]

'v_' + str(fc)

'v_a'
0 Kudos
JoeBorgione
MVP Emeritus

I'm not familiar with the CreateDataBaseView_management() function, but error 837 catches my attention.  Again, just my personal preference, but I would create a varable to hold the database name parameter and insert it rather than using the os.path.join() method where you do. I just like laying things out...

That should just about do it....
0 Kudos
FrédéricPRALLY
Esri Contributor

Hi,

Try following code :

import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server Database sde.sde"
featureclasses = arcpy.ListFeatureClasses()

for fc in featureclasses:
 arcpy.arcpy.CreateDatabaseView_management(
 "Database Connections\\Server Database sde.sde", "v_" + str(fc.split('.')[2]), "select * from " + fc)
JoeBorgione
MVP Emeritus

I'm not the guy looking for a solution, but I'm sure he'll give it a spin....

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I think fc.split('.')[-1] will work with more platforms.