[GIS] How to convert from .shp to .csv using python and either gdal_translate of ogr2ogr

gdal-translateogr2ogrpython

I am trying to convert .shp files to csv using either gdal_translate or ogr2ogr and python but its not work. if i run the following from qgis its not giving an error but at the same time its not producing the output files. Please help. Below is what i was running:

import os
command = "ogr2ogr -f 'CSV'" + "C:\Users\zanamwe\Desktop\Desktop\Programming For GIS\shapefiles\lakes.csv"+ "C:\Users\zanamwe\Desktop\Desktop\Programming For GIS\shapefiles\lakes.shp"
os.system(command)

Best Answer

perhaps a couple of resources on this -

here's another stack question that may be a duplicate.

or portions of this page might be helpful -

in any case, it appears to be a two step process - first create the .dbf from the csv

then create the spatial information from a vrt file (small xml-like file with basic information about your data and output projection).

also - as an aside, it's possible there are other issues with the code above - i would assume spaces are needed between file names and other options. this could be easier to see using the format method -and almost forgot, backslashes would be problematic as well, at least without an 'r' to identify a string literal

import os
command = "ogr2ogr -f 'CSV' {} {}".format(r"C:\Users\zanamwe\Desktop\Desktop\Programming For GIS\shapefiles\lakes.csv",r"C:\Users\zanamwe\Desktop\Desktop\Programming For GIS\shapefiles\lakes.shp")
os.system(command)

or something like that.