我正在尝试提取一些数据,以便将数据输入到服务器中。我从一个web API中获得了这些数据,其中包含了很多对我来说都是垃圾的信息。我需要处理掉一大堆,但我不知道从哪里开始。我需要的数据一直到“Contherious”,然后再从"name":"Contherious“开始。And here's that link。我一直在做的大多数数据处理都是尝试使用正则表达式搜索来尝试处理,我能想到的唯一搜索是在我需要的名称和不需要的名称之间留一个空格,并在它们后面直接指向ID。我只是不清楚如何获取这些名字中的每一个,如果有任何帮助,我将不胜感激。
我试过了
DMG_DONE_FILE = "rawDmgDoneData.txt"
out = []
with open(DMG_DONE_FILE, 'r') as f:
line = f.readline()
while line:
regex_id = search('^+"name":"\s"+(\w+)+"id":',line)
if regex_id:
out.append(regex_id.group(1))
line = f.readline()我会收到错误,因为我通常不知道如何处理正则表达式搜索
发布于 2019-04-18 10:04:54
import sys
import json
# use urllib to fetch from api
# example here for testing is reading from local file
f=open('file.json','r')
data=f.read()
f.close()
entries = json.loads(data)现在,您有了一个可以轻松寻址的数据结构
例如,条目‘entries’‘name’
或者使用jq https://stedolan.github.io/jq/
cat file.json |jq '.entries[]| {name:.name,id:.id,type:.type,itemLevel:.itemLevel,icon:.icon,total:.total,activeTime:.activeTime,activeTimeReduced:.activeTimeReduced}'https://stackoverflow.com/questions/55737966
复制相似问题