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])
First, there's a nice way of posting code in GeoNet.
As for your error, first make sure you're building your paths correctly and that the feature class you're after actually exists. Also, check that last line in the code. You want to write each row to the csv, not fields.
What is the error message?
edit: if you want to write the value of the current row, reference it in the row object.
csvwriter.writerow(row[0]) # would write the value in the first field in your field list
I have the following and choose a random field name that is in one of the many feature classes I need returned.
I still have the error.
with open('TEST.csv', 'w') as csvfile: csvwriter = csv.writer(csvfile, delimiter=',', lineterminator='\n') fields = ['APN'] with arcpy.da.SearchCursor(s, fields) as s_cursor: for row in s_cursor: csvwriter.writerow(fields)
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)
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.
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...
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
The variable represents mxd/path/feaure class name, for all in a specified folder.
Example: C:\Users\me\Documents\xyz.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?
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)
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.
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.