Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除两个指定索引之间数量可变的索引_Python_Json_Python 3.x_Python Requests - Fatal编程技术网

Python 删除两个指定索引之间数量可变的索引

Python 删除两个指定索引之间数量可变的索引,python,json,python-3.x,python-requests,Python,Json,Python 3.x,Python Requests,所以我第一次尝试使用json,这是我从kanka.ioAPI中提取的。我试图删除'entry'和'section'或'entry_parsed'之间的任何索引,以便确定ID是否属于字符或属性,并仅将字符名附加到列表中 为了在python tutor的实时编程模式下进行测试,我缩短了将json转换成的列表 # Request data from URL response = requests.request("GET", url, headers=headers, data=p

所以我第一次尝试使用json,这是我从kanka.ioAPI中提取的。我试图删除'entry'和'section'或'entry_parsed'之间的任何索引,以便确定ID是否属于字符或属性,并仅将字符名附加到列表中

为了在python tutor的实时编程模式下进行测试,我缩短了将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
我尝试过不同的变体,其中条目后的索引为==this1、last1、next1,并且没有一个实际删除“entry”和“entry\u parsed”或“section”之间的索引对象。我也试过了

if rsplit[this1] == "entry":
      while not rsplit[next1] == "entry_parsed" or "section":

而且它仍然会打印出“null”或“Blue”等。根据评论中的信息,您需要执行以下操作:

向kanka.io API发出请求 将响应解析为JSON,需要一个字典列表 选择键为“entry\u parsed”的词典 为所有选定词典创建“名称”值列表 因此,您应该只保留发出请求的代码的第一行,而放弃其余的代码,并改用以下代码:

# 1. Request data from URL
response = requests.get(url, headers=headers, data=payload)

# 2. parse as JSON
data = response.json()

# 3. + 4. list of 'name' values for all dicts having 'entry_parsed'
names = [d['name'] for d in data if 'entry_parsed' in d]

1不使用requests.request'GET',您可以直接使用requests.GET..

我可以使用以下代码仅提取名称值以及'entity_id'和'tags'的值

# Request data from URL
response = requests.get(url, headers=headers)

rj = response.json()

# using .items() allowed me to keep the tuples together so I could call keys to get their values

ri = rj.items()

name=[]
enid=[]
tags=[]

for i in ri:
  for j in i:
    for k in j:
      # since there is string metadata at the beginning and end of ri I narrowed it down to only the dictionaries which contained the values I needed
      if type(k) == dict:
        name.append(k['name'])
        enid.append(k['entity_id'])
        tags.append(k['tags'])
由于数据保留为字典而不是字符串,我不需要使用“entity_parsed”或“section”来识别字符与属性,因为属性的“id”和“name”是“traits”键的值


非常感谢@mkrieger1为我指明了正确的方向

你是如何获得rsplit的?看起来它曾经是一个字典,您设法以某种方式将其转换为一个列表,现在您很难从列表中提取值,这在使用字典时非常容易?这就是我在代码中实际填充rsplit的内容来自URL的请求数据response=requests.requestGET,URL,headers=headers,data=payload Open data rtext=response.text Clean data punt=['{','}','\',':',',']rt=对于rtext中的项:如果punt中的项:rt+=str'',否则:rt+=stritem逐项列出文本字符串rsplit=rt.split yes。不要将JSON语法替换为空格并将字符串拆分为列表,而是将响应解析为JSON。您能否编辑问题并显示您希望得到的结果,而不是当前打印的结果?请参阅
# Request data from URL
response = requests.get(url, headers=headers)

rj = response.json()

# using .items() allowed me to keep the tuples together so I could call keys to get their values

ri = rj.items()

name=[]
enid=[]
tags=[]

for i in ri:
  for j in i:
    for k in j:
      # since there is string metadata at the beginning and end of ri I narrowed it down to only the dictionaries which contained the values I needed
      if type(k) == dict:
        name.append(k['name'])
        enid.append(k['entity_id'])
        tags.append(k['tags'])