Posts

Showing posts with the label Csv

Converting Pandas DataFrame To GeoDataFrame

Answer : Convert the DataFrame's content (e.g. Lat and Lon columns) into appropriate Shapely geometries first and then use them together with the original DataFrame to create a GeoDataFrame. from geopandas import GeoDataFrame from shapely.geometry import Point geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)] df = df.drop(['Lon', 'Lat'], axis=1) gdf = GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) Result: Date/Time ID geometry 0 4/1/2014 0:11:00 140 POINT (-73.95489999999999 40.769) 1 4/1/2014 0:17:00 NaN POINT (-74.03449999999999 40.7267) Since the geometries often come in the WKT format, I thought I'd include an example for that case as well: import geopandas as gpd import shapely.wkt geometry = df['wktcolumn'].map(shapely.wkt.loads) df = df.drop('wktcolumn', axis=1) gdf = gpd.GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) Update 201912: The official documentation at h...

Amazon Redshift - COPY From CSV - Single Double Quote In Row - Invalid Quote Formatting For CSV Error

Answer : It's 2017 and I run into the same problem, happy to report there is now a way to get redshift to load csv files with the odd " in the data. The trick is to use the ESCAPE keyword, and also to NOT use the CSV keyword. I don't know why, but having the CSV and ESCAPE keywords together in a copy command resulted in failure with the error message "CSV is not compatible with ESCAPE;" However with no change to the loaded data I was able to successfully load once I removed the CSV keyword from the COPY command. You can also refer to this documentation for help: http://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-data-conversion.html#copy-escape Unfortunately, there is no way to fix this. You will need to pre-process the file before loading it into Amazon Redshift. The closest options you have are CSV [ QUOTE [AS] 'quote_character' ] to wrap fields in an alternative quote character, and ESCAPE if the quote character is preceded by...

Bulk Convert DBF To CSV In A Folder ArcGIS 10.1 Using Python

Answer : I have only tested this very briefly (and with a limited variety of data), but this script demonstrates one way this might be accomplished: import arcpy import csv import os import codecs import cStringIO def batch_convert_dbf_to_csv(input_dir, output_dir, rename_func=None): """Converts shapefiles and standalone DBF tables within the input directory input_dir to CSV files within the output directory output_dir. An optional function rename_func may be used to manipulate the output file name.""" # Set workspace to input directory arcpy.env.workspace = input_dir # List shapefiles and standalone DBF tables in workspace tables = list_tables() # Only proceed if there actually exists one or more shapefiles or DBF tables if tables: # Create output directory structure make_output_dir(output_dir) # Loop over shapefiles and DBF tables for table in tables: # Generate ...

Convert XML File To Csv File Format In C#

Answer : using System.IO; using System.Xml.Serialization; You can do like this: public class Sequence { public Point[] SourcePath { get; set; } } using (FileStream fs = new FileStream(@"D:\youXMLFile.xml", FileMode.Open)) { XmlSerializer serializer = new XmlSerializer(typeof(Sequence[])); var data=(Sequence[]) serializer.Deserialize(fs); List<string> list = new List<string>(); foreach(var item in data) { List<string> ss = new List<string>(); foreach (var point in item.SourcePath) ss.Add(point.X + "," + point.Y); list.Add(string.Join(",", ss)); } File.WriteAllLines("D:\\csvFile.csv", list); } In an alternate way you can use leverage the power of XSLT to convert it, Steps Create an Xml stylesheet to convert xml to csv Use XslCompiledTransform() to convert get the csv string save the csv string to a file You may came up with an Xslt like this, call it data.xsl <?...

Create Kml From Csv In Python

Answer : You didn't answer the query above, but my guess is that the error is that you're not closing your output file (which would flush your output). f.close() use etree to create your file http://docs.python.org/library/xml.etree.elementtree.html It's included with Python and protects you from generating broken XML. (eg. because fname contained & , which has special meaning in XML.)