[GIS] Error while importing CSV File using PyQGIS

csvpyqgispythonqgis

Currently I am trying to import a CSV file using the python extension in QGIS and I have run into a problem that I can not seem to solve.

I am trying to do a basic import of a CSV file from a python code I created (See below) and every time the file loads the values in my attribute table become re-arranged (see image).

When I load the file from the "Create a Layer from a Delimited Text File" option it loads without a problem.

Here is a sample of my code:

uri = "file:///D:/MLB Stadiums/Test_R2/Player_Files/Espinosa_2014.csv?
       delimiter=%ss&xField=%s&yField=%s" % (",", "field_3", "field_4")
   vlayer = QgsVectorLayer(uri, "Espinosa", "delimitedtext")
   caps =vlayer.dataProvider().capabilities()

Here is a comparison of what it should look like after import and what it does look like.

enter image description here

Does anyone know why it is rearranging the values?

Best Answer

It's having problems with the letter s, because your code specifies that as a delimiter (delimiter=%ss is getting interpreted as "," and "s"). Try delimiter=%s.

Related Question