I have multiple lines but only one is written to the csv file. What am I missing from an except of my code.
s = (fullpath + lyr_source) with open('TEST.csv', 'w') as writeexcel: a = csv.writer(writeexcel) for line in:a.writerow ([line])
I changed it to just read the lyr_source yet still same error.
I don't think this is a valid path to a feature class
'C:\MY_TEMPLATES\CW\CW_ENV_FMSE_EDIT.mxdDatabase Connections\CW_ENV_uCW_ENV_vCW_ENV.sde\PW.EnvironmentalCompliance\PW.ServiceEstablishments'
You need to go directly to the sde connection file, then to your feature class. Don't use the MXD path first. The full path to a feature class should look like
'Database Connections\CW_ENV_uCW_ENV_vCW_ENV.sde\PW.EnvironmentalCompliance\PW.ServiceEstablishments'
I think this is what Darren is getting at. Put some print statements in there to write what's in your variables so you know what's happening and where it's failing.
Does this mean I need to restructure the loop syntax of the mxds to connection files first then mxd path ?
What exactly are you trying to do? It looks like you are exporting every feature class to a CSV from every MXD in a folder.
Yes, that is exactly what I am trying to do.
It seems like I am trying something that is difficult or not possible to do.
At least I know it isn't just me being a beginner.
Using the layer.dataSource as your input for the search cursor like Darren mentioned should do the trick. Here's what the last part should look like.
for layer in layers: if layer.isFeatureLayer: lyr_source = layer.dataSource lyr_name = layer.name.encode("utf8", "replace") csv_name = "{}.csv".format(lyr_name) with open(csv_name, "w") as csvfile: csvwriter = csv.writer(csvfile, delimiter=",", lineterminator="\n") with arcpy.da.SearchCursor(lyr_source, "*") as s_cursor: for row in s_cursor: csvwriter.writerow(row)
Keep these things in mind with how you've structured your code so far:
Thank you for the advice.
I went ahead and changed according to your code, yet still get the same code error.
I would not want the layer names, but rather the feature class name.
I want the fullpath which has the mxd name.
Ultimately, the print out would have the mxd name and feature class name.
There are subfolders and group layers.
Ultimately, the print out would have the mxd name and feature class name.
I'm not quite sure what you mean by "the print out."
I would see the "print out" (results) in the python shell which would be written to the csv file also.
You want to print every row in every CSV to the Python interpreter? Doesn't seem useful to me.
Maybe start smaller and get each individual piece working. There is no obvious reason why this should not be working for you.