Is it possible to save layer as dbf? Or even better convert .csv to .dbf? I have some .csv files, no geometry just attributes and I need it as .dbf as I want to edit them (I found that Qgis doesn't support editing .csv files). And I need to do it in pyQGis. I can do it in QGIS (simple Save layer as...)
2 Answers
So I solved it. It wasn't that hard. I open csv as "delimitedText" layer. Then I save this layer as "ESRI Shapefile". There is a trick. It fails to save, but it creates a .dbf file. Then I load it back as "ogr" file.
pos_csv = QgsVectorLayer("file:///"+folder_name+'/pos_file.csv',"Layer Name","delimitedtext")
QgsVectorFileWriter.writeAsVectorFormat(pos_csv, folder_name+'/pos', "System", None,"ESRI Shapefile")
new_pos = QgsVectorLayer(folder_name+'/pos.dbf',"Layer Name","ogr")
QgsMapLayerRegistry.instance().addMapLayer(new_pos)
there are few (paid and free) third party programs you can use to convert csv to dbf (just google csv to dbf) and a few (free) online tools.
Online: http://www.files-conversion.com/spreadsheet-converter.php http://dbfconv.com/
programs: http://www.dbf2002.com/csv-converter/convert-csv-to-dbf.html http://dbfview.com/csv-dbf.html
last depending on your level of comfort with python there are a few resources https://stackoverflow.com/questions/3897694/python-convert-csv-to-dbf https://stackoverflow.com/questions/4440791/convert-csv-file-into-dbf-using-python converting csv to dbf
good luck
-
Thanks, but not exactly what I was looking for. I need it in plugin, so any online tools aren't acceptable. I could use dbf modul, but it isn't standart python modul, so it would complicate things. I thought that when QGIS can do it directly, why shouldn't I in pyQGis. There is option in Save layer as "dbf"...– AerovAug 21, 2015 at 18:11