How to export .csv file from python and using pandas DataFrame - Stack Overflow
CSV Export using Python - Python Scripting - Vectorworks Community Board
Save Print output in csv file
Export results of print in csv file
A simpler solution would be to convert to dbf file format, in which case you you can use the out-of-the-box Table to Table (Conversion). This tool also allows you the freedom to select which fields to include as FieldMappings as well as directly outputting a .dbf file.
import arcpy
fc = r'C:\path\to\your\fc'
outws = r'C:\temp'
arcpy.TableToTable_conversion (fc, outws, 'outFile.dbf')
ArcGIS already has a tool to do this called "Export Feature Attributes to ASCII" which is in the Spatial Statistics--> Utilities toolbox.
The advantage of this tool over "Table to Table" is that you can 1) define your delimiter (space, comma, tab), 2) choose the fields you want to export and 3) choose whether or not to export your field names to the CSV file. It also happens to be a Python script, so you could copy that file and make your own variant of it very easily.
So, if you want to create a model or script to export only the most recent features based on "last edit date field", simply precede the "Export Feature Attributes to ASCII" tool with a "Select Layer by Attribute" tool where you call out the query you want to run.
HI I am trying to save a bunch of information to a CSV file. I am scraping submissions using a keyword search and I have the title, submission id and subreddit
submissionid= post.id
title = post.title
subreddit = post.subredditnow I want to save it to a CSV. how can I do that? Pandas apparently helps with this but not sure how to go about that.
I know the question is asking about your "csv" package implementation, but for your information, there are options that are much simpler — numpy, for instance.
import numpy as np
np.savetxt('data.csv', (col1_array, col2_array, col3_array), delimiter=',')
(This answer posted 6 years later, for posterity's sake.)
In a different case similar to what you're asking about, say you have two columns like this:
names = ['Player Name', 'Foo', 'Bar']
scores = ['Score', 250, 500]
You could save it like this:
np.savetxt('scores.csv', [p for p in zip(names, scores)], delimiter=',', fmt='%s')
scores.csv would look like this:
Player Name,Score
Foo,250
Bar,500
Use csv.writer:
import csv
with open('thefile.csv', 'rb') as f:
data = list(csv.reader(f))
import collections
counter = collections.defaultdict(int)
for row in data:
counter[row[0]] += 1
writer = csv.writer(open("/path/to/my/csv/file", 'w'))
for row in data:
if counter[row[0]] >= 4:
writer.writerow(row)