2

I'm trying to import some CSV with the same name of the file in a geopackage using PyQGIS before opening a project, but what I'm using doesn't work

I'm using this code:

from qgis.core import *
import glob
import os
def openProject():
 ruta = QgsProject.instance().absolutePath()
 tmp = ruta + '/tmp'
 try:
 for csv in glob.glob(tmp + '/*.csv'):
 rutacsv = 'file:///' + csv + '?delimiter=;'
 tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')
 opt = QgsVectorFileWriter.SaveVectorOptions()
 opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
 QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg|layername' + os.path.basename(csv).split('.csv', 1)[0], opt)

I tried also this code:

from qgis.core import *
import glob
import os
def openProject():
 ruta = QgsProject.instance().absolutePath()
 tmp = ruta + '/tmp'
 try:
 for csv in glob.glob(tmp + '/*.csv'):
 rutacsv = 'file:///' + csv + '?delimiter=;'
 tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')
 opt = QgsVectorFileWriter.SaveVectorOptions()
 opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
 opt.LayerName = os.path.basename(csv).split('.csv', 1)[0]
 QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg', opt)

Although if I use this code, the table is stored but with the name of the geopackage (cdau).

from qgis.core import *
import glob
import os
def openProject():
 ruta = QgsProject.instance().absolutePath()
 tmp = ruta + '/tmp'
 try:
 for csv in glob.glob(tmp + '/*.csv'):
 rutacsv = 'file:///' + csv + '?delimiter=;'
 tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')
 opt = QgsVectorFileWriter.SaveVectorOptions()
 opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
 QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg', opt)

But I'm not able to see where the mistake is.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jun 8, 2020 at 7:31
2
  • what "doesn't work"? Commented Jun 8, 2020 at 7:34
  • Sorry, the table is not created with the first two codes. With the third one is created with the same name as the geopackage, not with the name of the .csv file Commented Jun 8, 2020 at 7:37

1 Answer 1

2

the following code when run from the python window reads csvs from my \tmp folder and adds each of them to the cdau.gpkg (it doesn't create the cdau.gpkp that has to exist already), each layer when added to the gpkp is given the name of the original csv

import glob
import os
ruta = QgsProject.instance().absolutePath()
tmp = ruta +'/tmp'
for csv in glob.glob(tmp + '/*.csv'):
 rutacsv = 'file:///' + csv + '?delimiter=,'
 tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')
 opt = QgsVectorFileWriter.SaveVectorOptions()
 opt.EditionCapability = QgsVectorFileWriter.CanAddNewLayer
 opt.layerName = os.path.basename(csv).split('.csv', 1)[0]
 opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
 QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg', opt)
answered Jun 8, 2020 at 14:03
4
  • Thanks @ian for your answer, but if I add your code the table is imported in the geopackage, but with the name of the gpkg file. What I need is to import every csv in the tmp folder and name every new table with the name of the csv file. Commented Jun 8, 2020 at 16:14
  • Hi @RaúlCasado I've updated the code in my answer - it adds the csvs to the gpkg when run from the python window in QGIS 3.10 on Windows 10 Commented Jun 8, 2020 at 16:57
  • Checking your code I saw that in the option line where the name of the new table is defined I wrote opt.LayerName = os.path.basename(csv).split('.csv', 1)[0] with LayerName with a capital L, so I wrote the name of the function correctly (with lowercase l) and it works fine. Thanks @ian. Commented Jun 8, 2020 at 18:06
  • @RaúlCasado, no problem, glad it's working now! Commented Jun 8, 2020 at 19:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.