[Qgis-user] multiple csv file import

Salvatore Mellino salvatore.mellino at prismambiente.it
Tue Nov 15 13:34:04 PST 2022


I found this on line :

import glob, os

# Define path to  directory of your csv files
path_to_csv = "C:/File Path/"

# 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+"encoding=%s&delimiter=%s&xField=%s&yField=%s&crs=%s" % ("UTF-8",",", "Long", "Lat","epsg:4326")
    name=fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    QgsProject.instance().addMapLayer(lyr)

but it doesn’t work. I have csv files with lat long and value separated by space and without header 

> Il giorno 15 nov 2022, alle ore 21:22, Hugh Kelley <hghklly at gmail.com> ha scritto:
> 
> 
> Awesome thanks very much. glad I asked, you learn something new every day.
> 
>> On Tue, Nov 15, 2022 at 3:19 PM David Strip <qgis-user at stripfamily.net> wrote:
>> Starting with v2.1, ogr2ogr supports args X_POSSIBLE_NAMES, Y_POSSIBLE_NAMES which are strings with allowed wildcards (eg, Lon* ), or you can use field_1, field_2, etc to explicitly give the position of lat/lon.
>> It's explained on the driver page.  This StackExchange post shows csv to shapefile conversion and included Windows command line syntax for looping over files in a directory.
>> 
>>> On 11/15/2022 12:58 PM, Hugh Kelley wrote:
>>> David, this was my first thought when i saw this question as well.
>>> 
>>> however, I didn't look for very long but I haven't seen a way to tell ogr2ogr to read columns in a csv as the lat/lon and write those as points to the shapefile.  I generally write a csv to postgres as a non-spatial table and then process the lat lon columns with postgis. 
>>> 
>>>  Are there arguments for ogr2ogr that can do this?
>> 
> 
> 
> -- 
> Hugh Kelley       
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20221115/a6886dfc/attachment.htm>


More information about the Qgis-user mailing list