python - most Pythonic way of achieveing these 4 lines? -
i have csv cell value, row[13]
contains following text: point (174.29635 -41.60557)
i need strip out text , brackets, , convert 2 numbers float, each assigned var:-
geo_pair = row[13].replace("point (", "") geo_pair = geo_pair.replace(")", "") self.longitude, self.latitude = geo_pair.split(" ") self.longitude, self.latitude = float(self.longitude), float(self.latitude)
i'm pretty sure there cleaner way of doing this, , wondered knows doing do!
since format fixed , consists of prefix, data, , suffix, use slicing remove prefix , suffix: map(float, s[7:-1].split())
.
this clear , simple @ same time:
>>> s = "point (174.29635 -41.60557)" >>> longitude, latitude = map(float, s[7:-1].split())
this works upon sign changes or when number of decimal places changes.
and way, long not parsing tons of input, not matter way chose. it's matter of taste , not performance-critical in case. don't spend time :-).
Comments
Post a Comment