2

I am trying to export several layers as CSV-files since I need to analyze them in another software. However, I cannot get my code to work. I am new to Python. Currently working in QGis 3.10.9. I would like all the fields in the attribute table to get exported. Here is the code I am using:

file_path = "/Users/user/Desktop/gis/Vikter/NUTS_ID_AT111978Q1.gpkg"
layer = QgsVectorLayer(file_path, "NUTS_ID_AT111978Q1", "ogr")
QgsVectorFileWriter.writeAsVectorFormat(layer, "/Users/user/Desktop/gis/Excel/NUTS_ID_AT111978Q1.csv", "utf-8", None, "CSV", layerOptions = ['GEOMETRY=AS_XYZ'])

The error message I receive:

Traceback (most recent call last):
  File "/Applications/QGIS3.10.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python3.7/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 3, in <module>
TypeError: QgsVectorFileWriter.writeAsVectorFormat(): arguments did not match any overloaded call:
  overload 1: argument 4 has unexpected type 'NoneType'
  overload 2: argument 4 has unexpected type 'NoneType'
  overload 3: argument 3 has unexpected type 'str'
6
  • have you tried removing the 'r' before the filename, I'm pretty sure it only applies for windows path.
    – Al rl
    Commented Sep 10, 2020 at 15:55
  • Yes, I have. I have reworked the code a bit and tried again. My post is updated.
    – timpan
    Commented Sep 10, 2020 at 15:56
  • Can you try QgsVectorFileWriter.writeAsVectorFormat(layer, "/Users/user/Desktop/gis/Excel/NUTS_ID_AT111978Q1.csv", "utf-8", layerOptions = ['GEOMETRY=AS_XYZ'])
    – Al rl
    Commented Sep 10, 2020 at 16:01
  • That works but it becomes a .gpkg file.
    – timpan
    Commented Sep 10, 2020 at 18:52
  • then adding driverName = "CSV" should do the trick as it didn't accept the invalid crs provided.qgis.org/pyqgis/3.0/core/Vector/…
    – Al rl
    Commented Sep 10, 2020 at 19:32

1 Answer 1

2

Solution is :

QgsVectorFileWriter.writeAsVectorFormat(layer,
"/Users/user/Desktop/gis/Excel/NUTS_ID_AT111978Q1.csv",
"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])

using r".." in linux was part of the error are this is a shortcut for windows path formatting.

The second mistake was passing none as a projection. Specifying the drivename as it is a keyword argument as the layer option seemed to have solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.