Alex Alex - 3 months ago 9
Python Question

Regular expression for coordinates in Python

I don't have much experience with regexps, so need to ask the question here.

I'm uploading a database for a learning algorithm. There are five variables: iteration, label, vector of pixel intensities, vector of coordinates and bounding box cordinates.

I've uploaded vector of pixels this way:

with open(tracked_cow, 'r+') as f:
...
elif line % 5 == 2:
# regular expression
string_this_obs = [float(val) for val in re.findall(r"[\d.]+", vs)]
mean_intensities.append(string_this_obs)


to get a list of intensities for each observation in floats, which is exactly what I need. Coordinates for each observation are in format
[(v1,v2), (v3,v4)...]
, and I'd like to get something like
['(v1,v2)','(v3,v4)'...]
, where each input is a string.

EDIT: here is the raw data from the file :

10,
3,
[0.11198258, 0.24691357, 0.17487293, 0.46013072, 0.20174292, 0.16528684, 0.20348585, 0.15991287, 0.3205519, 0.16397965, 0.54306459, 0.10878723, 0.035439365, 0.11387073, 0.8691358, 0.27843139, 0.090777054, 0.065649971, 0.1697894, 0.12941177, 0.2556282, 0.10762528, 0.26187363, 0.10312274, 0.26550472, 0.069571532, 0.23805377, 0.10036311, 0.18707335, 0.15976763, 0.1828613, 0.38010171, 0.094262883, 0.39157587, 0.35410312, 0.093827158, 0.10777052, 0.10777051, 0.10079884, 0.20130719, 0.1029775, 0.20275964, 0.57981122, 0.26056644, 0.16180103, 0.21089324, 0.18445896, 0.15323168, 0.070007272, 0.14989108, 0.22716051, 0.58344227, 0.69876546, 0.13478577, 0.17037037, 0.17893973, 0.16092958, 0.98155409, 0.2771242, 0.0824982, 0.29092228, 0.089034133, 0.11314452, 0.07392884, 0.07770516, 0.074074082, 0.27102399, 0.10442992, 0.19419028, 0.20116195, 0.16325344, 0.10617284, 0.84647787, 0.5863471, 0.088017434, 0.16891794, 0.070007272, 0.088598408, 0.13493101, 0.18997823, 0.98779958, 0.071895428, 0.17748728, 0.19680466, 0.15700799, 0.49513438, 0.068409592, 0.96920842, 0.09440814, 0.90515625, 0.2878722, 0.03267974, 0.22120552, 0.26753813, 0.070007272, 0.11372551, 0.11532317, 0.29019612, 0.21161947, 0.37400147],
[(366, 732), (269, 759), (318, 739), (326, 790), (369, 771), (384, 775), (376, 744), (312, 739), (366, 737), (304, 736), (359, 758), (333, 773), (266, 728), (362, 728), (313, 767), (343, 759), (381, 777), (268, 731), (290, 751), (287, 740), (266, 760), (334, 745), (276, 728), (313, 751), (382, 766), (265, 751), (328, 755), (310, 748), (349, 730), (374, 759), (350, 743), (360, 756), (375, 782), (354, 754), (349, 738), (345, 747), (296, 789), (355, 790), (356, 778), (363, 730), (346, 772), (278, 753), (314, 765), (383, 723), (303, 734), (374, 778), (361, 770), (299, 733), (368, 732), (286, 737), (268, 762), (287, 769), (306, 770), (323, 748), (346, 787), (294, 757), (340, 751), (283, 771), (333, 738), (266, 737), (326, 740), (382, 772), (325, 751), (267, 741), (366, 759), (266, 732), (270, 758), (307, 782), (357, 781), (325, 755), (304, 733), (320, 780), (362, 749), (283, 775), (379, 773), (374, 730), (368, 732), (265, 748), (338, 767), (317, 736), (340, 784), (316, 788), (272, 728), (360, 770), (292, 762), (359, 756), (343, 778), (306, 767), (321, 784), (340, 725), (288, 724), (366, 789), (378, 735), (339, 735), (383, 753), (305, 780), (326, 773), (366, 750), (312, 729), (339, 738)],
Bbox(x0=723.0, y0=263.0, x1=794.0, y1=389.0),

Answer

Does this do what you're looking for?

re.findall(r'(\(\d+, \d+\))', coord_string)
Comments