【发布时间】:2021-03-07 19:35:10
【问题描述】:
所以我第一次尝试弄乱我从 kanka.io API 中提取的 json。我正在尝试删除 'entry' 与 'section' 或 'entry_parsed' 之间的所有索引,以便确定 ID 是否属于字符或属性,并仅将字符名称附加到列表中。
为了在 python 导师的现场编程模式下进行测试,我已经缩短了我将 json 转换为的列表本身。
# Request data from URL
response = requests.request("GET", url, headers=headers, data=payload)
# Open data
rtext=response.text
# Clean data
punct = ['{','}','[',']','\"',':',',']
rt = ""
for item in rtext:
if item in punct:
rt+=str(' ')
else:
rt+=str(item)
# Itemize string of text
rsplit = rt.split()
#rsplit = [
#'id', '260405', 'name', 'Frank', 'Burns', 'entry', 'null', 'entry_parsed', 'traits',
#'id', '260406', 'name', 'Henry', 'Blake', 'entry', 'null', 'entry_parsed', 'null', 'image', 'null',
#'id', '260407', 'name', 'Margret', 'Houlihan', 'entry', 'null', 'entry_parsed', 'null', 'image', 'true', 'is_private', 'true',
#'id', '260408', 'name', 'John', 'MacInyre', 'entry', '\\n<p>Graduate', 'of', 'Darthmouth.<\\/p>\\n<p>\\u00a0<\\/p>\\n', 'entry_parsed',
#'id', '260409', 'name', 'Walter', 'O\'Reilly', 'entry', 'null', 'entry_parsed', 'null', 'image', 'image_full', 'https',
#'id', '260410', 'name', 'Benjiam', 'Franklin', 'Pierce', 'entry', 'null', 'entry_parsed', 'null', 'image', 'image_full', 'https',
#'id', '165148', 'name', 'Eyes', 'entry', 'Blue', 'section', 'appearance', 'is_private', 'false', 'default_order', '1',
#'id', '260411', 'name', 'Francis', 'Mulcahy', 'entry', 'null', 'entry_parsed', 'null',
#]
#########
# NAMES #
#########
# Append character names into list
this1=0
# Cycle throught all the words
while this1 < len(rsplit):
next1 = this1+1
last1 = this1-1
# Stop at the first element after 'name'
if rsplit[last1] == "name":
# Read and concatenate elements until the element 'entry'
while rsplit[next1] != "entry":
nextword = rsplit[next1]
rsplit[this1]+='_'+nextword
# Remove redundant elements by replacing next with last
rsplit[next1]=rsplit[this1]
rsplit.remove(rsplit[this1])
# Remove words inbetween entry and (entry_parsed or section)
if rsplit[this1] == "entry":
while rsplit[next1] != ("entry_parsed" or "section"):
rsplit.remove(rsplit[descWord])
print(rsplit[this1:next1+4])
this1+=1
我希望它从打印行打印的是
['Frank_Burns', 'entry', 'entry_parsed', 'traits']
['Henry_Blake', 'entry', 'entry_parsed', 'null']
['Margret_Houlihan', 'entry', 'entry_parsed', 'null']
['John_MacInyre', 'entry','entry_parsed']
["Walter_O'Reilly", 'entry', 'entry_parsed', 'null']
['Benjiam_Franklin_Pierce', 'entry', 'entry_parsed', 'null']
['Eyes', 'entry', 'section', 'appearance']
['Francis_Mulcahy', 'entry', 'entry_parsed', 'null']
我尝试了不同的变体,其中 entry 后的索引是 == this1、last1、next1,但它们都没有真正删除 'entry' 和 'entry_parsed' 或 'section' 之间的索引对象。我也试过了
if rsplit[this1] == "entry":
while not rsplit[next1] == "entry_parsed" or "section":
它仍然不断打印出'null'或'Blue'等。
【问题讨论】:
-
您究竟是如何获得
rsplit的?看起来它曾经是一本字典,但您设法以某种方式将其转换为列表,现在您很难从列表中提取值,而这在使用字典时会很容易? -
这是我用来在我的代码中实际填充 rsplit 的内容。 `# 从 URL 请求数据 response = requests.request("GET", url, headers=headers, data=payload) # 打开数据 rtext=response.text # 清理数据 punct = ['{','}','[ ',']','\"',':',','] rt = "" for item in rtext: if item in punct: rt+=str(' ') else: rt+=str(item) # Itemize文本字符串 rsplit = rt.split()'
-
是的。不要用空格替换 JSON 语法并将字符串拆分为列表,而是将响应解析为 JSON。
-
您能否编辑问题并显示您想要得到的结果而不是当前打印的结果?
标签: python json python-3.x python-requests