Sunday, 5 November 2017

python - PyQGIS import csv- QGIS 3.4


This is seemingly pretty simple. I have a csv file I want to add to the map. It is comma delimited and there is no lat/lon information. I will join it to a shapefile later. I used other examples from stackoverflow and this documentation in the QGIS cookbook to do it. I can add it with the following code:


uri = 'file:///Users/ep9k/Desktop/SandraMonson/CountByZip.csv?delimiter=,'
csv_file = QgsVectorLayer(uri, 'Patient Data', 'delimitedtext')
QgsProject.instance().addMapLayer(csv_file)


This adds the file to the map. But in the attribute table it reads all data as column headers. The attribute table looks like this: enter image description here


ZipCode, PatientCount, StateOrProvince should be the column headers, with data below. How can I read the first line as column headers and the rest as data in those columns?


Another thought I have, after looking at the QGIS cookbook documentation, I went into PyQGIS API documentation and into Module>Core Library> and looked for QgsVectorLayer and QgsProject, and it seems counterintuitive as a csv file is not a vector layer.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...