最困难的部分是解析该字符串,这相当简单:
>>> assignedzip_numberpolygon_str = '-71.064483,42.347181 -71.062819,42.346781 -71.061012,42.346099 -71.060932,42.345734 -71.060514,42.345511 -71.060566,42.344569 -71.05971,42.344816 -71.057818,42.346189 -71.05595,42.346601 -71.05683,42.347199 -71.05572,42.34714 -71.052975,42.351364 -71.055483,42.352548 -71.056277,42.35286 -71.056276,42.352891 -71.056476,42.353006 -71.05678,42.353126 -71.056999,42.353208 -71.057292,42.353273 -71.05914,42.35459 -71.06048,42.3555 -71.061772,42.356065 -71.062008,42.355811 -71.062073,42.355276 -71.06287,42.35567 -71.06331,42.35521 -71.064136,42.353623 -71.06423,42.35339 -71.06456,42.35238 -71.063717,42.352295 -71.06258,42.3524 -71.06276,42.35148 -71.063056,42.350902 -71.064275,42.348432 -71.064483,42.347181'
>>> assignedzip_numberpolygon = [map(float, coord.split(",")) for coord in assignedzip_numberpolygon_str.split()]
>>> assignedzip_numberpolygon
[[-71.064483, 42.347181], [-71.062819, 42.346781], [-71.061012, 42.346099], [-71.060932, 42.345734], [-71.060514, 42.345511], [-71.060566, 42.344569], [-71.05971, 42.344816], [-71.057818, 42.346189], [-71.05595, 42.346601], [-71.05683, 42.347199], [-71.05572, 42.34714], [-71.052975, 42.351364], [-71.055483, 42.352548], [-71.056277, 42.35286], [-71.056276, 42.352891], [-71.056476, 42.353006], [-71.05678, 42.353126], [-71.056999, 42.353208], [-71.057292, 42.353273], [-71.05914, 42.35459], [-71.06048, 42.3555], [-71.061772, 42.356065], [-71.062008, 42.355811], [-71.062073, 42.355276], [-71.06287, 42.35567], [-71.06331, 42.35521], [-71.064136, 42.353623], [-71.06423, 42.35339], [-71.06456, 42.35238], [-71.063717, 42.352295], [-71.06258, 42.3524], [-71.06276, 42.35148], [-71.063056, 42.350902], [-71.064275, 42.348432], [-71.064483, 42.347181]]
然后你所要做的就是复制 dict 并专门处理这种情况:
def parse_assignedzip_numberpolygon(s):
return [map(float, coord.split(",")) for coord in s.split()]
def proc_data_dict(d):
new_d = d.copy()
new_d['assignedzip_numberpolygon'] = parse_assignedzip_numberpolygon(d['assignedzip_numberpolygon'])
return new_d
这是一个示例用法:
dataset = [{'assignedzip_longitude': -71.064699000000005,
'assignedzip_numberpolygon': u'-71.064483,42.347181 -71.062819,42.346781 -71.061012,42.346099 -71.060932,42.345734 -71.060514,42.345511 -71.060566,42.344569 -71.05971,42.344816 -71.057818,42.346189 -71.05595,42.346601 -71.05683,42.347199 -71.05572,42.34714 -71.052975,42.351364 -71.055483,42.352548 -71.056277,42.35286 -71.056276,42.352891 -71.056476,42.353006 -71.05678,42.353126 -71.056999,42.353208 -71.057292,42.353273 -71.05914,42.35459 -71.06048,42.3555 -71.061772,42.356065 -71.062008,42.355811 -71.062073,42.355276 -71.06287,42.35567 -71.06331,42.35521 -71.064136,42.353623 -71.06423,42.35339 -71.06456,42.35238 -71.063717,42.352295 -71.06258,42.3524 -71.06276,42.35148 -71.063056,42.350902 -71.064275,42.348432 -71.064483,42.347181',
'assignedzip_pk': u'02111',
'name': u'Any Name',
'store_latitude': 42.353197999999999,
'store_longitude': -71.057495000000003},
{'assignedzip_latitude': 42.360587000000002,
'assignedzip_longitude': -71.065447000000006,
'assignedzip_numberpolygon': u'-71.077051,42.358732 -71.075592,42.355871 -71.075243,42.355601 -71.075054,42.355336 -71.073294,42.355803 -71.073326,42.356427 -71.072439,42.358829 -71.0711,42.358812 -71.071075,42.358278 -71.070669,42.357699 -71.06981,42.35788 -71.070192,42.358828 -71.0677,42.35887 -71.06507,42.35886 -71.065089,42.358327 -71.064355,42.358356 -71.063953,42.358406 -71.062857,42.35865 -71.062873,42.359362 -71.062888,42.361159 -71.061415,42.360908 -71.061495,42.358804 -71.060177,42.359171 -71.060143,42.359052 -71.059765,42.359093 -71.059424,42.359539 -71.05974,42.359735 -71.060275,42.360502 -71.060931,42.360849 -71.060833,42.361341 -71.059718,42.361851 -71.058526,42.362498 -71.057623,42.362647 -71.057993,42.362995 -71.058227,42.364403 -71.058577,42.366966 -71.059112,42.367823 -71.060008,42.369051 -71.063087,42.368704 -71.062968,42.368323 -71.064329,42.369163 -71.064398,42.368983 -71.065072,42.36883 -71.065397,42.368993 -71.067653,42.368983 -71.069793,42.369082 -71.070245,42.368701 -71.070569,42.368283 -71.070816,42.367967 -71.071583,42.368362 -71.071628,42.36829 -71.071625,42.368228 -71.07091,42.367872 -71.075705,42.361646 -71.077051,42.358732',
'assignedzip_pk': u'02114',
'name': u'Test Name',
'store_latitude': 42.357497000000002,
'store_longitude': -71.058494999999994}]
for d in dataset:
new_d = proc_data_dict(d)
print new_d
输出是:
{'store_latitude': 42.353198, 'name': u'Any Name', 'assignedzip_numberpolygon': [[-71.064483, 42.347181], [-71.062819, 42.346781], [-71.061012, 42.346099], [-71.060932, 42.345734], [-71.060514, 42.345511], [-71.060566, 42.344569], [-71.05971, 42.344816], [-71.057818, 42.346189], [-71.05595, 42.346601], [-71.05683, 42.347199], [-71.05572, 42.34714], [-71.052975, 42.351364], [-71.055483, 42.352548], [-71.056277, 42.35286], [-71.056276, 42.352891], [-71.056476, 42.353006], [-71.05678, 42.353126], [-71.056999, 42.353208], [-71.057292, 42.353273], [-71.05914, 42.35459], [-71.06048, 42.3555], [-71.061772, 42.356065], [-71.062008, 42.355811], [-71.062073, 42.355276], [-71.06287, 42.35567], [-71.06331, 42.35521], [-71.064136, 42.353623], [-71.06423, 42.35339], [-71.06456, 42.35238], [-71.063717, 42.352295], [-71.06258, 42.3524], [-71.06276, 42.35148], [-71.063056, 42.350902], [-71.064275, 42.348432], [-71.064483, 42.347181]], 'assignedzip_longitude': -71.064699, 'store_longitude': -71.057495, 'assignedzip_pk': u'02111'}
{'store_latitude': 42.357497, 'name': u'Test Name', 'assignedzip_numberpolygon': [[-71.077051, 42.358732], [-71.075592, 42.355871], [-71.075243, 42.355601], [-71.075054, 42.355336], [-71.073294, 42.355803], [-71.073326, 42.356427], [-71.072439, 42.358829], [-71.0711, 42.358812], [-71.071075, 42.358278], [-71.070669, 42.357699], [-71.06981, 42.35788], [-71.070192, 42.358828], [-71.0677, 42.35887], [-71.06507, 42.35886], [-71.065089, 42.358327], [-71.064355, 42.358356], [-71.063953, 42.358406], [-71.062857, 42.35865], [-71.062873, 42.359362], [-71.062888, 42.361159], [-71.061415, 42.360908], [-71.061495, 42.358804], [-71.060177, 42.359171], [-71.060143, 42.359052], [-71.059765, 42.359093], [-71.059424, 42.359539], [-71.05974, 42.359735], [-71.060275, 42.360502], [-71.060931, 42.360849], [-71.060833, 42.361341], [-71.059718, 42.361851], [-71.058526, 42.362498], [-71.057623, 42.362647], [-71.057993, 42.362995], [-71.058227, 42.364403], [-71.058577, 42.366966], [-71.059112, 42.367823], [-71.060008, 42.369051], [-71.063087, 42.368704], [-71.062968, 42.368323], [-71.064329, 42.369163], [-71.064398, 42.368983], [-71.065072, 42.36883], [-71.065397, 42.368993], [-71.067653, 42.368983], [-71.069793, 42.369082], [-71.070245, 42.368701], [-71.070569, 42.368283], [-71.070816, 42.367967], [-71.071583, 42.368362], [-71.071628, 42.36829], [-71.071625, 42.368228], [-71.07091, 42.367872], [-71.075705, 42.361646], [-71.077051, 42.358732]], 'assignedzip_longitude': -71.065447, 'store_longitude': -71.058495, 'assignedzip_latitude': 42.360587, 'assignedzip_pk': u'02114'}