How to use CSV module write to multiple lines.

9945
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
DarrenWiens2
MVP Honored Contributor

I'm not quite sure what you're trying to achieve here. What is the variable 's' - a file path string? If so, Python won't know to open it and read it, you need to actually read the file or table, for example, with a SearchCursor.

0 Kudos
DevinUnderwood2
Occasional Contributor

The variable represents mxd/path/feaure class name, for all in a specified folder.

Example: C:\Users\me\Documents\xxxxx.mxdDatabaseConnections\PW_vCW_uCW.sde\PW.WDistribution\PW.wControlValve

I assigned a variable and trying to write it to csv file. You mentioned I need to read it. Is search cursor what I need or do I read it another way?

0 Kudos
DarrenWiens2
MVP Honored Contributor

Yes, if you're reading a simple system file, like a CSV, you can do so using something like csv.reader. GIS feature classes are somewhat more complex and specific, so you need to use a cursor that knows how to crack into the file structure. One such cursor for reading a feature class table is a SearchCursor​, although there are other options outside of arcpy.

I'll just add an example why iterating through a file path string doesn't do much of anything:

>>> s = r'C:\junk.shp' # the string itself is an iterable
... for char in s: # loop through the items in s
...     print char # prints characters in s one at a time
...     
C
:
\
j
u
n
k
.
s
h
p
DevinUnderwood2
Occasional Contributor

Yes, thank you for clarifying.

I am trying this out and realizing that I can easily read an existing file and write to another, but my returned featureclass results requires something else.

I set a variable and I would get unusual results like you posted, vertically one character per cell,etc.

I will read up on Search Cursor and I am one step closer to what I need.  I thought Search Cursor is only to return field names of feature classes and tables, which I want the feature class name and path.

To be cont...

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Devin, are you using the csv module still? If so, check Blake's post, or maybe clarifying the title a bit might help.

0 Kudos
DevinUnderwood2
Occasional Contributor

Yes, I am still trying csv module. I realize I have been going back and forth in a different post, for trying to find the easiest way.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Try writerows() instead of writerow(). That works if you have all of your data in one Python iterable like a list of lists or tuples (no need to use it with a for loop). I use it when writing out from a SQL query. It also looks like you are missing a line to write your field names. And like Darren mentioned, your just using a path as your data, you need something to actually read it.

Here's the code I use for writing a feature class table out to a CSV line by line.

import arcpy
import os
import csv

# Environment variables
workingDir = r"C:\temp"
workingGDB = os.path.join(workingDir, "MyGeodatabase.gdb")
inputTable = os.path.join(workingGDB, "MyInputTable")
outputCSV = os.path.join(workingDir, "MyOutput.csv")

# Create CSV
with open(outputCSV, "w") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=',', lineterminator='\n')
    ## Write field name header line
    fields = ['FirstField','NextField','AndThirdExample']
    csvwriter.writerow(fields)
    ## Write data rows
    with arcpy.da.SearchCursor(inputTable, fields) as s_cursor:
        for row in s_cursor:
            csvwriter.writerow(row)
DevinUnderwood2
Occasional Contributor

I modified with arcpy.da.SearchCursor(inputTable, fields) as s_cursor:

to with arcpy.da.SearchCursor(s,"") as s_cursor:

I don't want field names and the s is a variable I set for all feature classes/paths for mxds in a specified folder.

I get the following error when I run my script cannot open 'C:\CW\......

Does anyone know why? Thanks for your help in advance.

0 Kudos
BlakeTerhune
MVP Regular Contributor

You have to give at least one field to the search cursor to get data for. You can leave out writing the field names in the CSV by removing line 16 from my code example above.

csvwriter.writerow(fields)

0 Kudos