Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created July 1, 2026 06:03
Show Gist options
  • Select an option

  • Save aspose-com-gists/088fbd56081881af9c583ace62ac395a to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/088fbd56081881af9c583ace62ac395a to your computer and use it in GitHub Desktop.
Complete Guide: Shapefile to CSV in Python

Complete Guide: Shapefile to CSV in Python

Learn to convert Shapefile to CSV in Python using Aspose.GIS for Python via .NET. You will learn the required installation steps, key Aspose.GIS features, handling null values, and performance tips for large datasets, plus a full working code example.

Read the full guide here: https://blog.aspose.com/gis/complete-guide-shapefile-to-csv-in-python/

# Complete working code for Shapefile to CSV conversion
import clr
import sys
import os
# Add reference to the Aspose.GIS assembly (adjust the path if necessary)
clr.AddReference(r"C:\Program Files\Aspose\Aspose.GIS.dll")
from Aspose.Gis import ShapefileReader, CsvWriter, Feature
def shapefile_to_csv(input_shp: str, output_csv: str):
# Ensure the input file exists
if not os.path.isfile(input_shp):
raise FileNotFoundError(f"Input Shapefile not found: {input_shp}")
# Open the Shapefile for reading
with ShapefileReader.Open(input_shp) as reader:
# Retrieve the attribute schema (field names)
field_names = [field.Name for field in reader.Schema.Fields]
# Initialize CSV writer with headers
with CsvWriter(output_csv, field_names) as csv_writer:
# Iterate over each feature in the Shapefile
for feature in reader:
# Extract attribute values, handling nulls
row = []
for name in field_names:
value = feature.Attributes.GetValue(name)
# Replace None (null) with empty string for CSV compatibility
row.append("" if value is None else str(value))
# Write the row to the CSV file
csv_writer.WriteRow(row)
print(f"Conversion completed: {output_csv}")
if __name__ == "__main__":
# Example usage
shapefile_path = r"data/sample.shp"
csv_path = r"output/sample.csv"
shapefile_to_csv(shapefile_path, csv_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment