I have written a plugin using the Plugin_Builder tool. It takes a CSV, rearranges it and adds a column of WKT coordinates in the style LINESTRING(x1 y1,x2 y2). It then writes it as a new .csv. This works fine. I can then manually import it as 'delimited text' and it loads as a normal visible, interrogatable layer. What I am trying to do is get the script to load the CSV into QGIS directly after creating the file. After writing the CSV to my chosen location (out_filename), I have added the following:

            ##adding the file to QGIS
            #uri ='file:///C://Users//Desktop//test2.csv?delimiter=%s&crs=epsg:2154&wktField=%s' % (",", "WKT")
            layer = self.iface.addVectorLayer(uri, "my_layer", "delimitedtext")
            #QgsVectorFileWriter.writeAsVectorFormat(layer,out_filename + '.shp', 'CP1250', None, 'ESRI Shapefile')

The # lines are me trying experiments, none of which work. I wasn't sure if it was just not finding the file, hence the explicit name. A layer is loaded but with no contents. The QGIS error message is:

2022-06-04T14:09:44     WARNING    Errors in file C://Users//Desktop//test2.csv
2022-06-04T14:09:44     WARNING    File cannot be opened or delimiter parameters are not valid

The layer that is created has a red warning icon next to the name. If I click it and redirect it to the file location, it opens as expected.

The headers and first two rows look like:

Name,ID,Seq,Heading,Start time,End time,SOL Fix,EOL Fix,Wind,Waves,Format,Type,N/A,N/A,N/A,N/A,N/A,N/A,N/A,Nav,USBL,Comments,Status,Length,Project,Client,Area,Contractor,Vessel,Datum,EPSG,WKT
EQ22307-01001-04,1,,22.355,,,,,,,Prime,Analogue,-,-,-,-,-,-,-,-,-, ,Virgin,8.644,,,,,,ED50 - UTM 29N/-9E,,"LINESTRING(525182.750 6756568.136,528470.372 6764562.356)"
EQ22307-01005-08,2,,22.355,,,,,,,Prime,Analogue,-,-,-,-,-,-,-,-,-, ,Virgin,8.644,,,,,,ED50 - UTM 29N/-9E,,"LINESTRING(525205.871 6756558.628,528493.493 6764552.848)"
  • do you need ' around your delimiter in the url?
    – Ian Turton
    Jun 4, 2022 at 12:46
  • if you mean uri='out_filename?&wktField=%s'%('wkt') instead of ("wkt"), I have just tried and got the same error. I have aslo tried with WKT insted of wkt to match the capitalisation on my CSV file - same error.
    – WillH
    Jun 4, 2022 at 12:56
  • 1
    If I understand your workflow correctly, you read a csv into QGIS, calculate some geographical properties from the csv-file, converts this to WKT, saves as a csv and then re-reads it into QGIS to display it. Could it be possible to in stead just read the initial csv, calculate the geometry and display it and then, if needed, be able to save the layer afterwards? Jun 18, 2022 at 11:14
  • @MortenSickel that is something I'm looking at as an alternative. I have been keeping it as a .CSV (as a WatchFile) because it is easier for me to do a lot of the other things I do with it (outside QGIS) if it stays as an Excel interactable file. If I had to export it from QGIS every time I need to do QGIS-external work then it becomes just as slow as importing the .CSV each time I make a change.
    – WillH
    Jun 18, 2022 at 11:30
  • @WillH, Then it makes sense, Jun 18, 2022 at 11:37

1 Answer 1


After learning how to import a X/Y point pairs, I rejigged the code to get it to work for WKT.CSV. I think my problem above was primarily the file path/uri. This code now works:

    vlayer = QgsVectorLayer(uri, project+"_QC", "delimitedtext")

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.