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

Importing multiple CSVsCSV files in QGIS 3.18 with PyQGIS

Let's say I have a folder C:/Users/MyPC/Desktop/TestC:/Users/MyPC/Desktop/Test, where there's multiple CSVs in it. I'm trying to import all CSVs into QgisQGIS at once. I've tried to modify (the encoding part) the method from 'https://gis.stackexchange.com/questions/212910/loading-multiple-csv-files-into-qgis-2-10'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)
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 saysthe guidebook says and import a "single""single" csv into QGIS just in case something was wrong from the beginning, and yes, it still grabs an empty files.

'https://docs.qgis.org/3.4/pdf/en/QGIS-3.4-PyQGISDeveloperCookbook-en.pdf' P.10

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")
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?

Importing multiple CSVs in QGIS 3.18 with PyQGIS

Let's say 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 'https://gis.stackexchange.com/questions/212910/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.

'https://docs.qgis.org/3.4/pdf/en/QGIS-3.4-PyQGISDeveloperCookbook-en.pdf' P.10

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?

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?

Source Link
Cookie
  • 375
  • 1
  • 7

Importing multiple CSVs in QGIS 3.18 with PyQGIS

Let's say 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 'https://gis.stackexchange.com/questions/212910/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.

'https://docs.qgis.org/3.4/pdf/en/QGIS-3.4-PyQGISDeveloperCookbook-en.pdf' P.10

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?