Skip to main content
2 of 2
added 60 characters in body; edited tags; edited title
Taras
  • 34.1k
  • 4
  • 73
  • 148

Importing multiple CSV files in QGIS 3.18 with PyQGIS

I have a folder C:/Users/MyPC/Desktop/Test, where there's multiple CSVs in it. I'm trying to import all CSVs into QGIS at once. I've tried to modify (the encoding part) the method from Loading multiple CSV files into QGIS 2.10

The final code I used is: (It grabs all the files in the folder but appears empty)

import glob, os

path_to_csv = "C:\\Users\\MyPC\\Desktop\\Test" 

os.chdir(path_to_csv)  

for fname in glob.glob("*.csv"):  
    uri = "file:///" + path_to_csv + fname + "?encoding=%s&?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % ("UTF-8", ";", "longtitude", "latitude")
    name = fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    QgsProject.instance().addMapLayer(lyr)

I thought I might as well use what the guidebook says and import a "single" csv into QGIS just in case something was wrong from the beginning, and yes, it still grabs an empty files.

The code I use:

uri = "file:///C:/Users/MyPC/Desktop/Test/test.csv?encoding={}&?delimiter={}&xField={}&yField={}&crs={}".format("UTF-8", ",", "Longtitude", "Latitude", "espg:4326")
vlayer = QgsVectorLayer(uri, "Name", "delimitedtext")

Where did I go wrong?

Cookie
  • 375
  • 1
  • 7