Parsing a Text File with Python

Python is very handy to perform quick (and useful) tasks such as parsing a text file.

In my case, I have a problem with the QGIS project files, since they *hardcode the absolute paths of RasterLite databases*.

You can read here the description of my problem:

Since I found no better solution, and I need to move my project to other computers, I decided to write a script that replaces the path of the database on the project file (in XML) by the path of the database on the target computer.

It is a simple script, and you only need to pay attention to two things:

– the script must be located under the same folder as the QGIS project file.

– the database must be located in the parent directory (the program will perform a check to see if this is true)

Initially I passed my ‘hardcoded path’ as a variable, but then, to make it useful for other people I made the script accept the hardcoded path as an argument (if you don’t set it, it will default to my hardcoded path: ‘/home/joana/git/GISforEAF/data

So you can access the script help like this:

>python -h

Usage: [options]

-h, --help show this help message and exit

-p PATH, --path=PATH path to be replaced

And you can pass a path as an argument, like this:

>python -p /home/joana/tmp

This means that the database path, that was pointing to ‘/home/joana/tmp’ in the original path, will now point to the parent directory in the current computer (whatever that is)

The python code is bellow, in the hope that it will be useful to someone:

#!/usr/bin/env python
import os
import fnmatch
import fileinput
import sys
from optparse import OptionParser

#****** Script to amend the rasterlite paths on QGIS project files *******

# Copyright 2013 Joana Simoes

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <;.

# For this script to work, the project files must have been saved on *
# '/home/joana/git/GISforEAF/data' (or amend variable savedDir) *
# Also the rasterlite database must be one directory up (..)

def parse_log(fname,rasterDir):
s = open(fname).read()
old = s
s = s.replace(savedDir, rasterDir)

if (old != s):
print ' Found!'
f = open(fname, 'w')
print ' Replaced!'

parser = OptionParser()
parser.add_option("-p", "--path", dest="pathname",
help="path to be replaced", metavar="PATH")

(options, args) = parser.parse_args()



with open(upDir + '/tutorials_raster.sqlite'): pass
except IOError:
print 'Error: the rasterlite database was not found in the parent directory!.'

print 'Rasterlite database found on ' + upDir + '/tutorials_raster.sqlite'


if (options.pathname==None):

print 'String to be replaced: ' + savedDir
print 'Replace string: ' + upDir

#print os.getcwd()

for fname in dirList:
if fnmatch.fnmatch(fname, '*.qgs'):
print ' parsing ' + fname + '...'


One thought on “Parsing a Text File with Python

  1. Pingback: RasterLite e paths absolutas | Modelação Geográfica, Cidades e Ordenamento do Território

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s