trantimus
trantimus

Reputation: 55

Retrieving coordinates from a text file with Python

I have a text file with coordinates which is formatted like this:

158,227,,396,226,,487,138,,473,48,,145,55,,139,133,,159,229
413,289,,547,156,,526,26,,140,10,,85,147,,115,245,,415,291
295,175,,292,293

Using strip an re.sub I got a result like this:

import re
with open('test.txt') as f:
    for line in f:
        line.strip()
        line = re.sub(r',,', ' ', line)
        print line

158,227 396,226 487,138 473,48 145,55 139,133 159,229

413,289 547,156 526,26 140,10 85,147 115,245 415,291

295,175 292,293

I want to use those pairs of coordinates (x,y) but I don't know how I could do it.

Upvotes: 2

Views: 303

Answers (2)

Marcus Müller
Marcus Müller

Reputation: 36337

Your string substitution isn't helping you getting numbers out of this. To be honest, it complicates things.

The most intuitive approach here would definitely be:

  1. splitting all lines at the ,
  2. combining the list items to tuples

so let's do this. NOTE: due to stackexchange unilaterally changing their terms, code posted here since 1.1.2016 is MIT licensed, unless I say so. I do say so. The code below is CC-by-SA, and if you don't believe me, it's not even code, it's part of my text, which is CC-by-SA in every part. Thanks SE for making noting this necessary.

first, let's do the splitting

with open('test.txt') as f:
    for line in f:
        items = line.split()

Now, let's combine the coordinates into tuples. We only care about the first and second coordinate, then jump three forward, and so on. That's what the [start:stop:step] syntax does. zip takes multiple iterables and combines them to tuples, element-wise.

tuples_of_strings = zip(coordinates[0::3], coordinates[1::3])

Then, you might want to generate Point(x,y) objects of that

points = [Point(float(tup[0]), float(tup[1])) for tup in tuples_of_strings]

Upvotes: 1

Remi Guan
Remi Guan

Reputation: 22282

Why do you need regex in this case?

>>> with open('file') as f:
...     t = f.read().replace('\n', ',,')

>>> [[int(j) for j in i.split(',')] for i in t.split(',,') if i]
[[158, 227],
 [396, 226],
 [487, 138],
 [473, 48],
 [145, 55],
 [139, 133],
 [159, 229],
 [413, 289],
 [547, 156],
 [526, 26],
 [140, 10],
 [85, 147],
 [115, 245],
 [415, 291], 
 [295, 175], 
 [292, 293]]

Upvotes: 1

Related Questions