Initial Question Here Followed with Developments Below
I'm aware of the question posted here but my 120 csv files representing UK postcode centroids do not have x and y headers and dive straight into eastings and northings. Please see the code below which assumes coordinates are defined by columns 'x' and 'y'.
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')
QgsMapLayerRegistry.instance().addMapLayer(lyr)
My data:
I'm only interested in the 1st, 3rd and 4th columns. I've also attached a screenshot of what the data looks like in excel below the example code. How do I define columns 3 and 4 in python to replace 'x' and 'y' referred in this question?
AL1 1AG 10 515487 206498
AL1 1AJ 10 515491 206410
AL1 1AR 10 516270 205897
AL1 1AS 10 515005 206908
AL1 1AT 10 516131 206148
AL1 1AU 10 516141 206088
AL1 1BH 10 514836 206657
AL1 1BU 10 514643 206972
AL1 1BX 10 514619 206893
AL1 1BY 10 514582 206986
AL1 1BZ 10 514667 207047
AL1 1DG 10 514450 206754
Development 1.0
With a response to my initial question, I plugged the code in the editor but received the following error:
exec(open('C:/Users/JAMESB~1/AppData/Local/Temp/tmp3xtgxhmj.py'.encode('utf-8')).read())
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.2\apps\Python36\lib\code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 4
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
My Script:
import glob, os
# Define path to directory of your csv files
path_to_csv = "C:\Users\>mynamehere<\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 + "?useHeader=no&delimiter=%s&crs=epsg:27700&xField=3&yField=4" % (",")
name = fname.replace('.csv', '')
lyr = QgsVectorLayer(uri, name, 'delimitedtext')
QgsMapLayerRegistry.instance().addMapLayer(lyr)
I have placed all the csv files into a folder called "csv folder" on my desktop. Can you help me solve my issue? Sorry for my ignorance in python, this is a first for me!
Development 1.1
I corrected my file path with forward slashes and used the code below but still to no avail:
Script
import glob, os
# Define path to directory of your csv files
path_to_csv = "C:/Users/myname/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
for fname in glob.glob("*.csv"):
uri = "file:///" + path_to_csv + fname + "?useHeader=no&delimiter=%s&crs=epsg:27700&xField=3&yField=4" % (",")
name = fname.replace('.csv', '')
lyr = QgsVectorLayer(uri, name, 'delimitedtext')
QgsMapLayerRegistry.instance().addMapLayer(lyr)
Error
exec(open('C:/Users/JAMESB~1/AppData/Local/Temp/tmp4ejnw3eo.py'.encode('utf-8')).read())
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.2\apps\Python36\lib\code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 13, in <module>
NameError: name 'QgsMapLayerRegistry' is not defined