【问题标题】:Can not enter characters in dictionary with python script无法使用python脚本在字典中输入字符
【发布时间】:2016-08-03 03:17:54
【问题描述】:

我在python中通过检索json格式的数据制作了一个字典。

[{"id":"1","kingdom":"Metazoa ","phylum":"Arthropoda ","class":"Insecta ","order":"Hemiptera ","family":"Belostomatidae ","genus":"Abedus"},<br>
{"id":"2","kingdom":"Viridiplantae ","phylum":"Streptophyta ","class":"unclassified_Streptophyta ","order":"Pinales ","family":"Pinaceae ","genus":"Abies"}]

当我访问数据时,我试图只获取 genus 的值为 Abies 的数据,但我得到了错误

ValueError : 以 10 为底的 int ( ) 的无效文字:'Abies'

但是如果我输入一个数值,我会得到对应于json的“id”的数据。

这是我的脚本:

import urllib2
import simplejson

title = raw_input("find taxonom: ")
print "key: ",title 

response = urllib2.urlopen("http://localhost/csv/taxo.json")
data = simplejson.load(response)
get = int(str(title))
print data[get]

如何让它显示与属输入匹配的每个数据的“id”、“kingdom”、“phlyum”、“class”等?

【问题讨论】:

  • int(str(title)) 不可能是正确的:它说“取标题,将其设为字符串,并将此字符串设为整数”。我想标题不是整数。

标签: python json python-2.7


【解决方案1】:

您遇到的错误是因为您试图将包含非数值的字符串转换为整数。要解决此问题,请删除 get = int(str(title))


您拥有的数据是一个列表。列表使用数字索引来访问列表中不同位置的元素。要打印genus 值,您必须这样做:

print data[1]['genus']

请注意,这针对列表中的第二个字典。要为第一个字典打印 genus 的值,您必须将 1 更改为 0


要打印包含genus 匹配标题值的每个字典的值,请执行以下操作:

for attr_map in data:
    if attr_map['genus'] == title:
        print attr_map

程序运行示例:

>>> import json
>>> buffer = '[{"id":"1","kingdom":"Metazoa ","phylum":"Arthropoda ","class":"Insecta ","order":"Hemiptera ","family":"Belostomatidae ","genus":"Abedus"},{"id":"2","kingdom":"Viridiplantae ","phylum":"Streptophyta ","class":"unclassified_Streptophyta ","order":"Pinales ","family":"Pinaceae ","genus":"Abies"}]'
>>> data = json.loads(buffer)
>>> data
[{u'kingdom': u'Metazoa ', u'family': u'Belostomatidae ', u'class': u'Insecta ', u'id': u'1', u'phylum': u'Arthropoda ', u'genus': u'Abedus', u'order': u'Hemiptera '}, {u'kingdom': u'Viridiplantae ', u'family': u'Pinaceae ', u'class': u'unclassified_Streptophyta ', u'id': u'2', u'phylum': u'Streptophyta ', u'genus': u'Abies', u'order': u'Pinales '}]
>>>
>>> title = raw_input("find taxonom: ")
find taxonom: Abies
>>> for attr_map in data:
...     if attr_map['genus'] == title:
...         print attr_map
... 
{u'kingdom': u'Viridiplantae ', u'family': u'Pinaceae ', u'class': u'unclassified_Streptophyta ', u'id': u'2', u'phylum': u'Streptophyta ', u'genus': u'Abies', u'order': u'Pinales '}

【讨论】:

  • 可能不是 OP 想要的。他想通过genus 键进行搜索。
  • 感谢@SuperSaiyan,是的,我想找到关键字属。
  • 谢谢@smac89,如果我删除get = int(str(title))有错误TypeError: list indices must be integers, not str
  • @dwiyanto 删除它并使用我发布的解决方案或来自另一个答案的解决方案。首要问题是您拥有的数据是一个列表,因此只能由整数索引。此外,int 类只接受仅包含数值的字符串
  • 我尝试打印值没有数据会出现,无法读取if attr_map['genus'] == title:所以结果空白
【解决方案2】:

这将找到第一个匹配的条目:

print next(x for x in data if x["genus"] == title)

【讨论】:

  • 如果您要使用next 而不是[&lt;list comprehension&gt;][0],为什么不直接使用next(&lt;genexp&gt;)
  • 在写next 时忘记取下它们。现在修复它。谢谢指出!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 2019-01-25
  • 2022-11-03
  • 2017-12-31
  • 1970-01-01
相关资源
最近更新 更多