2

By the following code from the other post, I was able to load multiple CSV files into QGIS.

import glob, os

# Define path to  directory of your csv files
path_to_csv = "C:/Users/You/Desktop/csv folder/"  

# Set current directory to path of csv files
os.chdir(path_to_csv) 

# Find each .csv file and load them as vector layers
for fname in glob.glob("*.csv"):  
    uri = "file:///" + path_to_csv + fname + "?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % (",", "x", "y")
    name = fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    QgsProject.instance().addMapLayer(lyr)

However, not all my file headers are in English: instead of x,y I have: "經度", "緯度" so I tried to declare the encoding like common python files. As a result, QGIS load up the files but failed to get the x,y and the name directory.

# -*- coding: UTF-8 -*-
import glob, os

# Define path to  directory of your csv files
path_to_csv = "C:/Users/You/Desktop/csv folder/"  

# Set current directory to path of csv files
os.chdir(path_to_csv) 

# Find each .csv file and load them as vector layers
for fname in glob.glob("*.csv"):  
    uri = "file:///" + path_to_csv + fname + "?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % (",", "x", "y") #THE "x" AND "Y" is not in ENGLISH
    name = fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    QgsProject.instance().addMapLayer(lyr)

Where did I go wrong?

The one I load with ordinary ctrl+shift+T : enter image description here

The one that failed with python script: enter image description here

2 Answers 2

2

To achieve what you want, you need to also encode the field names for longitude/latitude and it will works (tested using your sample file name at https://gist.github.com/ThomasG77/1aa1bf66d847be043a246df8bf74793e You just need to change path_to_csv to your own path)

import glob
import os
from urllib.parse import quote


# Define path to  directory of your csv files
path_to_csv = "your_path"
x_field = '經度' # longitude
y_field = '緯度' # latitude

# Set current directory to path of csv files
os.chdir(path_to_csv)

for fname in glob.glob("*.csv"):
    uri = "file:///" + path_to_csv + quote(fname) + "?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % (",", quote(x_field), quote(y_field))
    # Uncomment below line and comment above line
    # if when you open the layer table you see garbage
    # characters as it will deal with UTF-8 encoding
    # using additional `encoding=UTF-8` option
    # uri = "file:///" + path_to_csv + quote(fname) + "?delimiter=%s&crs=epsg:4326&encoding=UTF-8&xField=%s&yField=%s" % (",", quote(x_field), quote(y_field))
    name = fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    QgsProject.instance().addMapLayer(lyr)
0

Try this:

# -*- coding: UTF-8 -*-
import glob, os
import urllib.parse

# Define path to  directory of your csv files
path_to_csv = "/home/rafd/Downloads/test_data/"
#print ('encoding in use:'+ sys.getfilesystemencoding())
# Set current directory to path of csv files
os.chdir(path_to_csv) 

# Find each .csv file and load them as vector layers
for fname in os.listdir():
    if fname.endswith(".csv"):
        print('This file is named: '+urllib.parse.quote_plus(fname))
        uri =  'file://'+path_to_csv +urllib.parse.quote_plus(fname) +'?type=csv&maxFields=10000&detectTypes=yes&xField=x&yField=y&crs=EPSG:27700&spatialIndex=no&subsetIndex=no&watchFile=no' 
        name = fname.replace('.csv', '')
        lyr = QgsVectorLayer(uri, name, 'delimitedtext')
        #print('importing: '+uri)
        QgsProject.instance().addMapLayer(lyr)

On a Linux box it works also with CSVs whose filename contain chinese characters ('經度.csv'); try it out on your OS.

For further import options, the official reference is:

https://qgis.org/pyqgis/master/core/QgsVectorLayer.html

7
  • 1
    Hi, appreciated your answer but this doesn't quite match the question. For better understanding, I'm using Chinese letters so it's ( ",", "經度", "緯度"), but since the whole file is not declared its encoding (Even if I write # -- coding: UTF-8 -- at the first line), QGIS cannot read the file directory.
    – Cookie
    Aug 23, 2021 at 13:41
  • 1
    I see: I thought "not all my files are in English (file name, x,y parameter)" meant they are not expressed in the "English" notation of CSVs (i.e. with decimal point separator). I edited your original question to make that clear. Aug 23, 2021 at 14:01
  • Also see the edited answer, with the encoding parameter Aug 23, 2021 at 14:11
  • 1
    Could you put complete name of one file in Chinese and its two first lines?
    – xunilk
    Aug 23, 2021 at 15:59
  • Filename(with abs path): 'C:/Users/Name/Desktop/File/鳥類.csv' First Line : ID,OccurrenceUUID,最後修改時間,物種學名,物種中文名,經度,緯度,界,綱,科 (經度,緯度 = longitude/latitude)
    – Cookie
    Aug 24, 2021 at 0:39

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.