How to use CSV module write to multiple lines.

12576
31
05-26-2016 11:57 AM
DevinUnderwood2
Occasional Contributor

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])
0 Kudos
31 Replies
DevinUnderwood2
Occasional Contributor

I changed it to just read the lyr_source yet still same error.

0 Kudos
BlakeTerhune
MVP Regular Contributor

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.

DevinUnderwood2
Occasional Contributor

Does this mean I need to restructure the loop syntax of the mxds to connection files first then mxd path ?

0 Kudos
BlakeTerhune
MVP Regular Contributor

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.

0 Kudos
DevinUnderwood2
Occasional Contributor

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.

0 Kudos
BlakeTerhune
MVP Regular Contributor

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:

  • What happens if the same layer name exists in more than one mxd?
  • Do you want to traverse down into subfolders looking for map docs?
  • Do you want to check for other data frames or group layers?
  • Do you want the field names written to the csv? (I recommend writing field names)
DevinUnderwood2
Occasional Contributor

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.

0 Kudos
BlakeTerhune
MVP Regular Contributor
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."

0 Kudos
DevinUnderwood2
Occasional Contributor

I would see the "print out" (results) in the python shell which would be written to the csv file also.

0 Kudos
BlakeTerhune
MVP Regular Contributor

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.

  1. Start by looping over your folder of MXDs and print the file name and layer names.
  2. In a new script, see if you can open a search cursor on one feature class and write the rows to CSV. This would have nothing to do with a map document.
  3. Finally, once you have those working, put those two bits together to export the CSV for every layer in every MXD.
0 Kudos