I have a text file containing coordinates but also comment lines such as :

#file description
#description point 1
id1 X1 Y1 Z1
#description point 2
id2 X2 Y2 Z2

and I want to import this file in QGIS 3.4. I need to build a regular expression for the delimiter that will allow QGIS to read lines with coordinates and pass comments beginning with #.

2 Answers 2


Can you be more specific why you need a regular expression here? Also, what do you want to do with the coordinates once you get them?

This code will read a text file and ignore any line that begins with an octothorpe (#), replaces the blank spaces between your coordinates with commas, and writes the results to a new text (CSV) file. This Python code does not rely on regular expressions. You will not need to rely on a regular expression to convert your GPS text file to a comma delimited text file.

You can replace the paths for f and fout to files and locations of your choosing. Save the newly edited code to a new text file with a .py extension. Execute the code using IDLE (comes with Python).

f =open(r"C:/gTemp/foo.txt","r")
fout = open(r"C:/gTemp/fooout.csv","w")
for line in f:
    if line[0] == "#":
        line = line.replace(" ",",")

After the new CSV file is created, use the Add Delimited Text option to display your data in QGIS. Depending on your CSV file you may need to open it in text editor and add the appropriate headers to the first row before adding it to QGIS.

  • I have data with this specific format (first line describing the content and then one line to describe the ground point and the next with its coordinates) and I want a regular expression to add delimited text layer in QGIS with the interface. I can't use Python because the importing tool requires a regular expression.
    – dmjf08
    Oct 28, 2019 at 7:29
  • You have no idea ?
    – dmjf08
    Oct 31, 2019 at 13:08
  • No, can you be more specific about how you are importing the text once you sort out data you need from the file? Why can't you use Python to read the text file, the write to useful information to another text file (a CSV) then import that to QGIS?
    – GBG
    Oct 31, 2019 at 14:40
  • I use a file containing GPS points with the previously specified format. I'm sure it's possible to build a regular expression to skip commented lines and parse the coordinates. In CSVs you can't have a commented line.
    – dmjf08
    Nov 6, 2019 at 9:08
  • Do you have any idea ?
    – dmjf08
    Nov 12, 2019 at 8:55

You can achieve this in QGIS without regular expressions, but with a virtual layer.

  1. Import the CSV, use Custom delimiters and check the box next to Space.

  2. Create a new virtual layer with this query, where csv is the name of the imported layer. The last line creates point only for lines that contain a number. Lines starting with # are skipped:

    select * , make_point (c.field_1,c.field_2) 
    from csv as c 
    where c.field_1 >0

Screenshot: imported csv with 5 features; virtual layer creates 2 points: enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.