Initial Question Below with Development at Bottom
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'.
Development
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!