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?