[Tutor] Need help with python script

Joel Goldstick joel.goldstick at gmail.com
Mon Aug 4 18:28:13 CEST 2014


On Mon, Aug 4, 2014 at 11:20 AM, P McCombs <mccombs at imperium.org> wrote:
> Sorry, I missed copying this to the list.
>
> On Aug 4, 2014 8:13 AM, "P McCombs" <mccombs at imperium.org> wrote:
>
>
>>
>>
>> On Jul 31, 2014 4:50 PM, "McKinley, Brett D." <BMCKINLEY at mawss.com> wrote:
>> >
>> > I would like to see if someone can help me with a python script.  I’m
>> > trying to export a file geodatabase feature class to csv file.  This is what
>> > I have so far:
>>
>> Does the code you attached execute successfully?
>> Are you running this as a tool,  or from the command line?
>> Be cautioned that arcpy is outside the scope of this mailing list,  but it
>> won't have much to do with your questions at first.
>
>>
>> >
>> >
>> > import arcpy
>> >
>> > import os
>> >
>> > import csv
>> >
>> > import domainvalues
>> >
>> >
>> >
>> >
>> >
>> > def export_to_csv(dataset, output, dialect):
>> >
>> >     """Output the data to a CSV file"""
>> >
>> >     # create the output writer
>> >
>> >     out_writer = csv.writer(open(output, 'wb'), dialect=dialect)
>> >
>> >     # return the list of field names and field values
>> >
>> >     header, rows = domainvalues.header_and_iterator(dataset)
>> >
>> >
>> >
>> >     # write the field names and values to the csv file
>> >
>> >     out_writer.writerow(map(domainvalues._encodeHeader, header))
>> >
>> >     for row in rows:

before you write row, you need to examine the appropriate fields to
filter out the fields you don't want, and order the rows by whichever
field determines newness.
Also, you probably want to output your header row to only include
columns you wish to write
>> >
>> >         out_writer.writerow(map(domainvalues._encode, row))
>> >
>> >
>> >
>> > if __name__ == "__main__":
>> >
>> >     # Get parameters
>> >
>> >     dataset_name = arcpy.GetParameterAsText(0)
>> >
>> >     output_file = arcpy.GetParameterAsText(1)
>> >
>> >     delim = arcpy.GetParameterAsText(2).lower()
>> >
>> >     dialect = 'excel'
>> >
>> >     if delim == 'comma':
>> >
>> >         pass
>> >
>> >     else:
>> >
>> >         dialect = 'excel-tab'
>> >
>> >     try:
>> >
>> >         export_to_csv(dataset_name, output_file, dialect)
>> >
>> >     except Exception as err:
>> >
>> >         arcpy.AddError('Error: {0}'.format(err))
>> >
>> >
>> >
>> >
>> >
>> > I would like for the script to export only certain fields and also
>> > export only the newest records.  As for now, it’s exporting everything.
>> >
>>
>> It is best to try to add one new feature on your own and attach the
>> result. You will get the best feedback that way.
>>
>> >
>> >
>> > Thanks
>> >
>> > Brett McKinley
>> >
>> >
>> >
>> >
>> >
>> >
>> > _______________________________________________
>> > Tutor maillist  -  Tutor at python.org
>> > To unsubscribe or change subscription options:
>> >https://mail.python.org/mailman/listinfo/tutor
>> >
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list