2

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?

2 Answers 2

1

There is just a tiny thing wrong, you forgot to connect your 'path_to_csv' and 'fname' with '//'

instead of:

uri = "file:///" + path_to_csv + fname + "?encoding=%s&?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % ("UTF-8", ";", "longtitude", "latitude")

it might be:

uri = "file:///" + path_to_csv + '//' + fname + "?encoding=%s&?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % ("UTF-8", ";", "longtitude", "latitude")

Also you can create your uri with os.path.abspath(), like this

uri = "file:///" + os.path.abspath(fname) + "?encoding=%s&?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % ("UTF-8", ";", "longtitude", "latitude")  
6
  • Hi, I've tried to implement your method and try it again. But it seems like the changes doesn't affect the result. I'll try to describe the problem here a bit more. The code seems to grab the files in the folder, which are listed in the Layer panel, while a warning sign appears claiming the sources are missing.(Layer data source could not be found) That's the point I'm very confused since the code certainly found the folder's location otherwise all the csv fils should not appears. :(
    – Cookie
    Commented Jun 22, 2021 at 8:39
  • Can you please show your csv files? Or share a piece of it?
    – Taras
    Commented Jun 22, 2021 at 8:49
  • I'll put the short demo file with 3 species in it in the following link. Since I'm using Traditional Chinese, the only keyword you need is '經度' and '緯度' in the attribute table, which represents the X and Y field. And assuming the given files are in a folder ' C:\\Users\\MyPC\\Desktop\\Test '
    – Cookie
    Commented Jun 22, 2021 at 10:44
  • drive.google.com/drive/folders/…
    – Cookie
    Commented Jun 22, 2021 at 10:50
  • I just checked your csv files, I think they do not have lat/long in it (Sorry I can not read Chinese), do not they? Therefore, you can not load them as a vector layer with a location, however, you can do it simply as a table: gis.stackexchange.com/a/133538/99589
    – Taras
    Commented Jun 22, 2021 at 10:57
1

There is a cool QGIS plugin that does so. Its name is CSV Batch Import.

It allows browsing for a CSV files directory. Then recursively import all CSV files inside that directory as layers in QGIS.

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.