Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Regex] Findall returns wrong number of hits

It's probably something obvious to seasoned Python programmers, but I can't figure out why re.findall() returns the wrong number of hits when using the following code :
import re,sys,locale

# Open file
f = open('input.gpx', 'r')

#strings = re.findall(r'<trk>',
strings = re.findall(r'<trk>.+?</trk>',


if strings:
	#27 instead of 348!
	print "Number of items : ", len(strings)
The file contains some French characters. Could it be that accented characters in the input file are preventing Python from reading the whole file?

FWIW, I'm using Python 2.7.14 on Windows.

Thank you.
post input.gpx justbeamit or sample of file content and wanted output.
(Aug-21-2018, 03:28 PM)Winfried Wrote: FWIW, I'm using Python 2.7.14 on Windows
You should be using Python 3.6/3.7 and pip installation under Windows
Then Unicode support is better,like eg for French characters.

So I removed 2.7.14, installed 3.7.0*, re-ran the code, and… same error: Wrong number of hits:

import re,sys,locale

# Open file
f = open('input.gpx', 'r')
#GOOD strings = re.findall(r'<trk>',

#Only 27!
#BAD strings = re.findall(r'<trk>.+?</trk>',

#Only 27!
p = re.compile(r'(<trk>.+?</trk>)',re.MULTILINE)
strings = p.findall(


if strings:
	#27 instead of 348!
	print("Number of items : " + str(len(strings)))
* Checked that "python -V" returns "Python 3.7.0"
It's the same problem for us,we don't know how input.gpx look or your wanted output from file.
A normal approach is to always read file with Unicode with UTF-8.
with open('input.gpx', encoding='utf-8') as f:
The code above does display the whole file, as expected.

Maybe it's some odd character stopping the regex module dead in its tracks.

If someone wants to give it a shot, here's the input file.

As a work-around, the gpxpy module works OK with my file.

Thanks for your help.
GPX files use XML namespaces.
Then can use lxml
from lxml import etree

NSMAP = {"gpx": ""}
tree = etree.parse("input.gpx")
for elem in tree.findall("gpx:trk", namespaces=NSMAP):
<Element {}trk at 0x4e469e0> <Element {}trk at 0x4e469b8> <Element {}trk at 0x4e46990> <Element {}trk at 0x4e46968> ....
Try to get something out.
>>> elem
<Element {}trk at 0x4e73760>
>>> elem[0].tag
>>> elem[0].text
'Viroflay - Vélizy - Arcueil'
Can also search PyPi for gpx parser.
Thanks, good to know.

I guess the file wasn't clean enough for the regex module to handle.
I couldn't find why the gpx module uses two "import" lines:

import gpxpy
import gpxpy.gpx
What's the difference?

Edit : Found it. It's a sub-module.

To learn about a module, launch Python, and do something like this:

>>> import gpxpy
>>> help(gpxpy)

>>> import gpxpy.gpx
>>> help(gpxpy.gpx)
In case another newbie stumbles on the same issue…

Gpxpy probably offers useful features anyway, but the reason the regex above didn't work, is simply that two switches are needed: "re.MULTILINE|re.DOTALL".

p = re.compile('<trk>.+?</trk>',re.MULTILINE|re.DOTALL)
m = p.findall(inputfile)
for item in m:

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [regex] Good way to parse variable number of items? Winfried 4 271 May-15-2020, 01:54 PM
Last Post: Winfried
  The "FindAll" Error BadWhite 6 359 Apr-11-2020, 05:59 PM
Last Post: snippsat
  Beginner question: lxml's findall in an xml namespace aecklers 0 183 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Unable to pull number using BeautifulSoup and Regex sarath_unrelax 2 268 Dec-18-2019, 08:09 PM
Last Post: sarath_unrelax
  python gives wrong string length and wrong character thienson30 2 380 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Number bug, calculation is wrong d3fi 2 483 Aug-27-2019, 05:29 AM
Last Post: perfringo
  Correct number wrong position func. albry 5 1,032 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  Issue with re.findall alinaveed786 8 1,227 Oct-20-2018, 09:28 AM
Last Post: volcano63
  Combining the regex into single findall syoung 0 751 May-28-2018, 10:11 AM
Last Post: syoung
  Print next dataset on list when user hits any key disruptfwd8 2 734 Apr-30-2018, 10:56 PM
Last Post: disruptfwd8

Forum Jump:

Users browsing this thread: 1 Guest(s)