3

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

1

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] == "#":
        pass
    else:
        line = line.replace(" ",",")
        fout.write(line)
fout.close()

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.

6
  • 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 '19 at 7:29
  • You have no idea ?
    – dmjf08
    Oct 31 '19 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 '19 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 '19 at 9:08
  • Do you have any idea ?
    – dmjf08
    Nov 12 '19 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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