Python如何将重复字符串后的数据提取出来

Python如何将重复字符串后的数据提取出来

大家好,我最近利用百度地图API的服务,将一对坐标点进行了路径纠偏,得到的数据不好处理,如下(数据存在txt中):
{"status":0,"message":"成功","total":613,"distance":38129.442980254,"toll\_distance":0,"low\_speed\_distance":0,"points":\[{"loc\_time":1538460540,"latitude":41.816901749842,"longitude":123.41365741449},{"loc\_time":1538464894,"latitude":41.824154192258,"longitude":123.4217009312},{"loc\_time":1538466753,"latitude":41.825368126128,"longitude":123.41660852133},{"loc\_time":1538469875,"latitude":41.51840611926,"longitude":122.89603529576},{"loc\_time":1538496133,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538496582,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538498302,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538498665,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538499573,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538499814,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538500602,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538500728,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538501898,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538503176,"latitude":41.802776894269,"longitude":123.41168415697},{"loc\_time":1538508443,"latitude":41.515108609009,"longitude":122.88899311986},{"loc\_time":1538530947,"latitude":41.6687088,"longitude":123.4072418,"mapmatch\_failed":1},{"loc\_time":1538532097,"latitude":41.79610062,"longitude":123.3974915,"mapmatch\_failed":1},{"loc\_time":1538532258,"latitude":41.79610062,"longitude":123.3974915,"mapmatch\_failed":1},{"loc\_time":1538532531,"latitude":41.79610062,"longitude":123.3974915,"mapmatch\_failed":1},{"loc\_time":1538533143,"latitude":41.78551956024,"longitude":123.38912090618,"transport\_mode":"驾车","speed":17.44354558393,"direction":30.227654288029},{"loc\_time":1538533143,"latitude":41.785568696679,"longitude":123.3891593023,"transport\_mode":"驾车","speed":17.44354558393,"direction":26.86189384904},{"loc\_time":1538533143,"latitude":41.785607236019,"longitude":123.38918548089,"transport\_mode":"驾车","speed":17.44354558393,"direction":24.726223729199},{"loc\_time":1538533143,"latitude":41.785695279774,"longitude":123.38923985599,"transport\_mode":"驾车","speed":17.44354558393,"direction":23.967144980402},{"loc\_time":1538533143,"latitude":41.785919920755,"longitude":123.38937378386,"transport\_mode":"驾车","speed":17.44354558393,"direction":23.637890583442},{"loc\_time":1538533143,"latitude":41.786228717286,"longitude":123.38955504182,"transport\_mode":"驾车","speed":17.44354558393,"direction":23.56316912175},{"loc\_time":1538533143,"latitude":41.786433592323,"longitude":123.3896748736,"transport\_mode":"驾车","speed":17.44354558393,"direction":17.385202606376},{"loc\_time":1538533143,"latitude":41.786491165713,"longitude":123.38969904911,"transport\_mode":"驾车","speed":17.44354558393,"direction":316.44573865616},{"loc\_time":1538533143,"latitude":41.786539281271,"longitude":123.38963769637,"transport\_mode":"驾车","speed":17.44354558393,"direction":311.57592598055},{"loc\_time":1538533143,"latitude":41.786584521891,"longitude":123.38956929957,"transport\_mode":"驾车","speed":17.44354558393,"direction":312.4031905124},{"loc\_time":1538533143,"latitude":41.786609178119,"longitude":123.38953309003,"transport\_mode":"驾车","speed":17.44354558393,"direction":308.6056619556},{"loc\_time":1538533143,"latitude":41.786664869885,"longitude":123.38943954555,"transport\_mode":"驾车","speed":17.44354558393,"direction":305.18544017058},{"loc\_time":1538533143,"latitude":41.786689723121,"longitude":123.38939226919,"transport\_mode":"驾车","speed":17.44354558393,"direction":301.12571260752},{"loc\_time":1538533143,"latitude":41.786700140085,"longitude":123.38936913323,"transport\_mode":"驾车","speed":17.44354558393,"direction":299.1372319567},{"loc\_time":1538533143,"latitude":41.786721045572,"longitude":123.38931883704,"transport\_mode":"驾车","speed":17.44354558393,"direction":295.73715758402},{"loc\_time":1538533143,"latitude":41.786768412326,"longitude":123.389187059,"transport\_mode":"驾车","speed":17.44354558393,"direction":293.99015495204},{"loc\_time":1538533143,"latitude":41.786831832582,"longitude":123.38899593185,"transport\_mode":"驾车","speed":17.44354558393,"direction":293.08611531603},{"loc\_time":1538533143,"latitude":41.786885862653,"longitude":123.38882593201,"transport\_mode":"驾车","speed":17.44354558393,"direction":19.793542597034},{"loc\_time":1538533143,"latitude":41.787528444972,"longitude":123.38913608922,"transport\_mode":"驾车","speed":17.44354558393,"direction":153.89353491069},{"loc\_time":1538533143,"latitude":41.787396879108,"longitude":123.38922255654,"transport\_mode":"驾车","speed":17.44354558393,"direction":111.46045169643},{"loc\_time":1538533143,"latitude":41.787330828325,"longitude":123.38944789801,"transport\_mode":"驾车","speed":17.44354558393,"direction":104.38519286058},{"loc\_time":1538533143,"latitude":41.78720268846,"longitude":123.39011795187,"transport\_mode":"驾车","speed":17.44354558393,"direction":104.41877313316},{"loc\_time":1538533143,"latitude":41.7870744197,"longitude":123.3907870486,"transport\_mode":"驾车","speed":17.44354558393,"direction":107.08197370874},{"loc\_time":1538533143,"latitude":41.786942775858,"longitude":123.39136159177,"transport\_mode":"驾车","speed":17.44354558393,"direction":107.21401931617},{"loc\_time":1538533143,"latitude":41.786810043864,"longitude":123.39193616346,"transport\_mode":"驾车","speed":17.44354558393,"direction":107.62581971421},{"loc\_time":1538533143,"latitude":41.786800270096,"longitude":123.39197742084,"transport\_mode":"驾车","speed":17.44354558393,"direction":107.28861171817},{"loc\_time":1538533143,"latitude":41.78675239547,"longitude":123.39218371015,"transport\_mode":"驾车","speed":17.44354558393,"direction":107.51398823584},{"loc\_time":1538533143,"latitude":41.786680651026,"longitude":123.39248862105,"transport\_mode":"驾车","speed":17.44354558393,"direction":116.76991667734},{"loc\_time":1538533143,"latitude":41.786622739418,"longitude":123.39264257822,"transport\_mode":"驾车","speed":17.44354558393,"direction":118.05624736486},{"loc\_time":1538533143,"latitude":41.78649357601,"longitude":123.39296760038,"transport\_mode":"驾车","speed":17.44354558393,"direction":122.20698118446},{"loc\_time":1538533143,"latitude":41.786148567663,"longitude":123.39370216533,"transport\_mode":"驾车","speed":17.44354558393,"direction":119.19404562454},{"loc\_time":1538533143,"latitude":41.786127605447,"longitude":123.39375248018,"transport\_mode":"驾车","speed":17.44354558393,"direction":121.66109013982},{"loc\_time":1538533143,"latitude":41.785878206616,"longitude":123.39429486875,"transport\_mode":"驾车","speed":17.44354558393,"direction":121.66415674218},{"loc\_time":1538533143,"latitude":41.785628772838,"longitude":123.39483726623,"transport\_mode":"驾车","speed":17.44354558393,"direction":122.21072796908},{"loc\_time":1538533143,"latitude":41.78538579714,"longitude":123.39535450921,"transport\_mode":"驾车","speed":17.44354558393,"direction":122.21229429185},{"loc\_time":1538533143,"latitude":41.785142804361,"longitude":123.39587175524,"transport\_mode":"驾车","speed":17.44354558393,"direction":208.35191910383},{"loc\_time":1538533143,"latitude":41.784915919279,"longitude":123.39570756219,"transport\_mode":"驾车","speed":17.44354558393,"direction":208.35771118062},{"loc\_time":1538533143,"latitude":41.784837989007,"longitude":123.39565115187,"transport\_mode":"驾车","speed":17.44354558393,"direction":208.3048943485},{"loc\_time":1538533143,"latitude":41.784797542864,"longitude":123.39562193924,"transport\_mode":"驾车","speed":17.44354558393,"direction":208.25027324413},{"loc\_time":1538533143,"latitude":41.784435486218,"longitude":123.39536103729,"transport\_mode":"驾车","speed":17.44354558393,"direction":203.28907779638},{"loc\_time":1538533143,"latitude":41.784102114868,"longitude":123.39516859352,"transport\_mode":"驾车","speed":17.44354558393,"direction":203.18162474487},{"loc\_time":1538533143,"latitude":41.783784549315,"longitude":123.39498622077,"transport\_mode":"驾车","speed":17.44354558393,"direction":203.41978509485},{"loc\_time":1538533143,"latitude":41.783538241466,"longitude":123.39484314314,"transport\_mode":"驾车","speed":17.44354558393,"direction":120.18239376921},{"loc\_time":1538533143,"latitude":41.783317417809,"longitude":123.39535232634,"transport\_mode":"驾车","speed":17.44354558393,"direction":119.09568824786},{"loc\_time":1538533143,"latitude":41.783048914182,"longitude":123.39599938365,"transport\_mode":"驾车","speed":17.44354558393,"direction":125.79056194758},{"loc\_time":1538533143,"latitude":41.782872560722,"longitude":123.39632741613,"transport\_mode":"驾车","speed":17.44354558393,"direction":117.03122559384},{"loc\_time":1538533143,"latitude":41.782724010388,"longitude":123.39671787308,"transport\_mode":"驾车","speed":17.44354558393,"direction":120.61318707749},{"loc\_time":1538533143,"latitude":41.782629882495,"longitude":123.39693120675,"transport\_mode":"驾车","speed":17.44354558393,"direction":118.81132998753},{"loc\_time":1538533143,"latitude":41.78255641848,"longitude":123.3971103296,"transport\_mode":"驾车","speed":17.44354558393,"direction":119.52361121354},{"loc\_time":1538533143,"latitude":41.782309098761,"longitude":123.39769599203,"transport\_mode":"驾车","speed":17.44354558393,"direction":119.48164386876},{"loc\_time":1538533143,"latitude":41.782061780012,"longitude":123.39828265162,"transport\_mode":"驾车","speed":17.44354558393,"direction":126.31217509107},{"loc\_time":1538533143,"latitude":41.781760172255,"longitude":123.3988330269,"transport\_mode":"驾车","speed":17.44354558393,"direction":121.77243163261},{"loc\_time":1538533143,"latitude":41.781492032747,"longitude":123.39941360437,"transport\_mode":"驾车","speed":17.44354558393,"direction":121.58936919473},{"loc\_time":1538533143,"latitude":41.78138037515,"longitude":123.39965709966,"transport\_mode":"驾车","speed":17.44354558393,"direction":121.53925528214},{"loc\_time":1538533143,"latitude":41.781250978991,"longitude":123.39993983142,"transport\_mode":"驾车","speed":17.44354558393,"direction":121.58866246188},{"loc\_time":1538533143,"latitude":41.781019374337,"longitude":123.40044491115,"transport\_mode":"驾车","speed":17.44354558393,"direction":134.08830513063},{"loc\_time":1538533143,"latitude":41.780912561966,"longitude":123.40059278204,"transport\_mode":"驾车","speed":17.44354558393,"direction":28.75222987771},{"loc\_time":1538533143,"latitude":41.780979649881,"longitude":123.40064214396,"transport\_mode":"驾车","speed":17.44354558393,"direction":29.872316767992},{"loc\_time":1538533143,"latitude":41.781416440653,"longitude":123.4009785876,"transport\_mode":"驾车","speed":17.44354558393,"direction":127.11144337562},{"loc\_time":1538533143,"latitude":41.781334708514,"longitude":123.40112345222,"transport\_mode":"驾车","speed":17.44354558393,"direction":127.44606744697},{"loc\_time":1538533143,"latitude":41.781241640476,"longitude":123.40128642182,"transport\_mode":"驾车","speed":17.44354558393,"direction":41.423941031009},{"loc\_time":1538533143,"latitude":41.781319931567,"longitude":123.40137906174,"transport\_mode":"驾车","speed":17.44354558393,"direction":37.066061166289},{"loc\_time":1538533143,"latitude":41.781477006758,"longitude":123.40153817386,"transport\_mode":"驾车","speed":17.44354558393,"direction":121.10251569561},{"loc\_time":1538533382,"latitude":41.781312216281,"longitude":123.40190447708,"transport\_mode":"驾车","speed":50.06249359834,"direction":19.005897941644},{"loc\_time":1538533679,"latitude":41.816446188639,"longitude":123.4181331886,"transport\_mode":"驾车","direction":19.005897941644},{"loc\_time":1538533682,"latitude":41.816446188639,"longitude":123.4181331886,"transport\_mode":"驾车","direction":19.005897941644},{"loc\_time":1538533718,"latitude":41.816446188639,"longitude":123.4181331886,"transport\_mode":"驾车","direction":19.005897941644},{"loc\_time":1538533818,"latitude":41.816446188639,"longitude":123.4181331886,"transport\_mode":"驾车","speed":7.9401973927289,"direction":350.42047096865},{"loc\_time":1538534006,"latitude":41.820124891944,"longitude":123.41730014552,"transport\_mode":"驾车","speed":68.218865406459,"direction":33.251966668727},{"loc\_time":1538534006,"latitude":41.820200163364,"longitude":123.41736637073,"transport\_mode":"驾车","speed":68.218865406459,"direction":32.918232588974},{"loc\_time":1538534006,"latitude":41.820249759837,"longitude":123.4174094545,"transport\_mode":"驾车","speed":68.218865406459,"direction":32.989815831},{"loc\_time":1538534006,"latitude":41.820437245361,"longitude":123.4175727671,"transport\_mode":"驾车","speed":68.218865406459,"direction":32.637485522078},{"loc\_time":1538534006,"latitude":41.820483877147,"longitude":123.41761284191,"transport\_mode":"驾车","speed":68.218865406459,"direction":33.817806073754},{"loc\_time":1538534006,"latitude":41.820691186942,"longitude":123.41779919203,"transport\_mode":"驾车","speed":68.218865406459,"direction":33.532214112416},{"loc\_time":1538534006,"latitude":41.820757657091,"longitude":123.41785830008,"transport\_mode":"驾车","speed":68.218865406459,"direction":33.106610971861},{"loc\_time":1538534006,"latitude":41.820891614989,"longitude":123.41797550893,"transport\_mode":"驾车","speed":68.218865406459,"direction":33.722907901669},{"loc\_time":1538534006,"latitude":41.820953129875,"longitude":123.41803060697,"transport\_mode":"驾车","speed":68.218865406459,"direction":67.559594317079},{"loc\_time":1538534006,"latitude":41.82096578086,"longitude":123.41807171129,"transport\_mode":"驾车","speed":68.218865406459,"direction":82.325921893847},{"loc\_time":1538534006,"latitude":41.820971318812,"longitude":123.41812686137,"transport\_mode":"驾车","speed":68.218865406459,"direction":116.33910430355},{"loc\_time":1538534006,"latitude":41.82091099078,"longitude":123.41829037428,"transport\_mode":"驾车","speed":68.218865406459,"direction":117.96549505513},{"loc\_time":1538534006,"latitude":41.82087526876,"longitude":123.41838065624,"transport\_mode":"驾车","speed":68.218865406459,"direction":114.54058616966},{"loc\_time":1538534006,"latitude":41.820850695215,"longitude":123.41845287614,"transport\_mode":"驾车","speed":68.218865406459,"direction":38.104793639268},{"loc\_time":1538534006,"latitude":41.821018239238,"longitude":123.4186291886,"transport\_mode":"驾车","speed":68.218865406459,"direction":38.135833221725},{"loc\_time":1538534006,"latitude":41.821318697106,"longitude":123.41894572469,"transport\_mode":"驾车","speed":68.218865406459,"direction":130.6309994537},{"loc\_time":1538534006,"latitude":41.820981953421,"longitude":123.41947234969,"transport\_mode":"驾车","speed":68.218865406459,"direction":187.50523372544},{"loc\_time":1538534006,"latitude":41.820564616621,"longitude":123.41939857102,"transport\_mode":"驾车","speed":68.218865406459,"direction":187.64976041295},{"loc\_time":1538534006,"latitude":41.820431831821,"longitude":123.41937463948,"transport\_mode":"驾车","speed":68.218865406459,"direction":189.60747443119},{"loc\_time":1538534006,"latitude":41.819952775132,"longitude":123.41926582776,"transport\_mode":"驾车","speed":68.218865406459,"direction":189.5017933214},{"loc\_time":1538534006,"latitude":41.819472721352,"longitude":123.41915801218,"transport\_mode":"驾车","speed":68.218865406459,"direction":188.12891491053},{"loc\_time":1538534006,"latitude":41.819108356657,"longitude":123.41908817731,"transport\_mode":"驾车","speed":68.218865406459,"direction":188.07440151177},{"loc\_time":1538534006,"latitude":41.818982575624,"longitude":123.41906423384,"transport\_mode":"驾车","speed":68.218865406459,"direction":187.70980577036}

这是一部分数据,想利用python或者matlab将其中的各个标识后面的数据提取出来,放入Excel中,以便进行后续的处理,该如何实现,感觉其实并不困那,属实是我太菜了,困扰了很久。


回答:

from json import loads

import pandas as pd

# xxx.txt是你txt文件的路径

with open('xxx.txt', 'r', encoding='utf-8') as f:

info = f.read()

if isinstance(info, str):

info = info.replace('\\', '')

info = loads(info)

points = info['points']

res = []

for p in points:

res.append([])

res[-1].append(p.get('loc_time'))

res[-1].append(p.get('latitude'))

res[-1].append(p.get('longitude'))

res[-1].append(p.get('transport_mode'))

res[-1].append(p.get('speed'))

res[-1].append(p.get('direction'))

df = pd.DataFrame(res, columns=['loc_time', 'latitude', 'longitude', 'transport_mode', 'speed', 'direction'])

df.to_excel('res.xlsx')

代码应该没什么问题, 希望能帮到你

以上是 Python如何将重复字符串后的数据提取出来 的全部内容, 来源链接: utcz.com/p/937826.html

回到顶部