Skip to main content
correction
Source Link
James B
  • 2.1k
  • 15
  • 32
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)
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)
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)
grammer
Source Link
James B
  • 2.1k
  • 15
  • 32

Initial Question Below with Development at Bottom

Initial Question Here Followed with Developments Below

Development

Development 1.0

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

Initial Question Below with Development at Bottom

Development

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!

Initial Question Here Followed with Developments Below

Development 1.0

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
development
Source Link
James B
  • 2.1k
  • 15
  • 32

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'.

enter image description here

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!

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'.

enter image description here

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'.

enter image description here

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!

Source Link
James B
  • 2.1k
  • 15
  • 32
Loading