我有这个JSON数据文件:
[
{
"Name": "A+",
"Type": "Array",
"Designed by": "Arthur Whitney"
},
{
"Name": "ActionScript",
"Type": "Compiled, Curly-bracket, Procedural, Reflective, Scripting, Object-oriented class-based",
"Designed by": "Gary Grossman"
},
{
"Name": "Ada",
"Type": "Compiled, Imperative, Procedural, Object-oriented class-based",
"Designed by": "Tucker Taft, Jean Ichbiah"
},
{
"Name": "Aldor",
"Type": "Iterative",
"Designed by": "Richard Dimick Jenks, Barry Trager, Stephen Watt, James Davenport, Robert Sutor, Scott Morrison"
},
{
"Name": "Alef",
"Type": "Curly-bracket",
"Designed by": "Phil Winterbottom"
},
{
"Name": "ALGOL",
"Type": "Compiled, Imperative, Procedural",
"Designed by": "Bauer, Bottenbruch, Rutishauser, Samelson, Backus, Katz, Perlis, Wegstein, Naur, Vauquois, van Wijngaarden, Woodger, Green, McCarthy"
},
{
"Name": "Ant",
"Type": "Interpreted",
"Designed by": "Apache Software Foundation"
},
{
"Name": "APL",
"Type": "Array, Interactive mode, Interpreted",
"Designed by": "Kenneth E. Iverson"
},
{
"Name": "AppleScript",
"Type": "Scripting",
"Designed by": "Apple Inc."
},
{
"Name": "AWK",
"Type": "Curly-bracket, Scripting",
"Designed by": "Alfred Aho, Peter Weinberger, Brian Kernighan"
},
{
"Name": "B",
"Type": "Curly-bracket",
"Designed by": "Ken Thompson"
},
{
"Name": "bash",
"Type": "Command line interface, Scripting",
"Designed by": "Brian Fox"
},
{
"Name": "BASIC",
"Type": "Imperative, Compiled, Procedural, Interactive mode, Interpreted",
"Designed by": "John George Kemeny, Thomas Eugene Kurtz"
},
{
"Name": "BCPL",
"Type": "Compiled, Curly-bracket, Procedural",
"Designed by": "Martin Richards"
},
{
"Name": "Blue",
"Type": "Imperative, Object-oriented class-based, Procedural",
"Designed by": "University of Sydney"
},
{
"Name": "C",
"Type": "Compiled, Curly-bracket, Imperative, Procedural",
"Designed by": "Dennis Ritchie"
},
{
"Name": "C#",
"Type": "Compiled, Curly-bracket, Iterative, Object-oriented class-based, Reflective, Procedural",
"Designed by": "Microsoft"
},
{
"Name": "C++",
"Type": "Compiled, Curly-bracket, Imperative, Metaprogramming, Object-oriented class-based, Procedural",
"Designed by": "Bjarne Stroustrup"
},
{
"Name": "Candle",
"Type": "Curly-bracket, Scripting",
"Designed by": "Henry Luo"
},
{
"Name": "Chapel",
"Type": "Array",
"Designed by": "David Callahan, Hans Zima, Brad Chamberlain, John Plevyak"
},
{
"Name": "ChucK",
"Type": "Curly-bracket, Object-oriented class-based, Reflective, Procedural",
"Designed by": "Ge Wang"
},
{
"Name": "Cilk",
"Type": "Curly-bracket",
"Designed by": "MIT"
},
{
"Name": "Clojure",
"Type": "Interactive mode, Reflective",
"Designed by": "Rich Hickey"
},
{
"Name": "COBOL",
"Type": "Compiled, Imperative, Procedural",
"Designed by": "Grace Hopper, William Selden, Gertrude Tierney, Howard Bromberg, Howard Discount, Vernon Reeves, Jean E. Sammet"
},
{
"Name": "Cobra",
"Type": "Compiled, Iterative, Object-oriented class-based, Procedural, Reflective",
"Designed by": "Charles Esterbrook"
},
{
"Name": "ColdFusion",
"Type": "Object-oriented class-based, Procedural, Scripting",
"Designed by": "Adobe Systems"
},
{
"Name": "Common Lisp",
"Type": "Compiled, Interactive mode, Object-oriented class-based, Reflective",
"Designed by": "Scott Fahlman, Richard P. Gabriel, Dave Moon, Guy Steele, Dan Weinreb"
},
{
"Name": "csh",
"Type": "Command line interface",
"Designed by": "Bill Joy"
},
{
"Name": "Curl",
"Type": "Compiled, Metaprogramming, Object-oriented class-based, Procedural, Reflective",
"Designed by": "Steve Ward"
},
{
"Name": "Cyclone",
"Type": "Curly-bracket",
"Designed by": "AT&T Labs"
},
{
"Name": "D",
"Type": "Compiled, Curly-bracket, Imperative, Metaprogramming, Object-oriented class-based, Procedural",
"Designed by": "Walter Bright, Andrei Alexandrescu"
},
{
"Name": "DASL",
"Type": "Object-oriented class-based, Curly-bracket, Procedural",
"Designed by": "Sun Microsystems Laboratories"
},
{
"Name": "Delphi",
"Type": "Compiled, Object-oriented class-based, Reflective",
"Designed by": "Apple, Niklaus Wirth, Anders Hejlsberg"
},
{
"Name": "E",
"Type": "Curly-bracket, Object-oriented class-based",
"Designed by": "Mark S. Miller"
},
{
"Name": "ECMAScript",
"Type": "Curly-bracket, Procedural, Reflective, Scripting",
"Designed by": "Brendan Eich, Ecma International"
},
{
"Name": "Eiffel",
"Type": "Compiled, Object-oriented class-based, Procedural, Reflective",
"Designed by": "Bertrand Meyer"
},
{
"Name": "Erlang",
"Type": "Compiled, Interactive mode",
"Designed by": "Joe Armstrong"
},
{
"Name": "Expect",
"Type": "Command line interface",
"Designed by": "Don Libes"
},
{
"Name": "F#",
"Type": "Interactive mode",
"Designed by": "Microsoft Research, Don Syme"
},
{
"Name": "Factor",
"Type": "Compiled",
"Designed by": "Slava Pestov"
},
{
"Name": "Fancy",
"Type": "Compiled, Interactive mode, Metaprogramming, Object-oriented class-based, Scripting",
"Designed by": "Christopher Bertels"
},
{
"Name": "Forth",
"Type": "Compiled, Interactive mode, Metaprogramming, Reflective",
"Designed by": "Charles H. Moore"
},
{
"Name": "Fortran",
"Type": "Array, Compiled, Imperative, Procedural",
"Designed by": "John Backus"
},
{
"Name": "Go",
"Type": "Compiled, Imperative, Procedural",
"Designed by": "Robert Griesemer, Rob Pike, Ken Thompson"
},
{
"Name": "Gosu",
"Type": "Compiled",
"Designed by": "Guidewire Software"
},
{
"Name": "Groovy",
"Type": "Compiled, Curly-bracket, Interpreted, Metaprogramming, Object-oriented class-based, Procedural, Reflective, Scripting",
"Designed by": "James Strachan"
},
{
"Name": "Hamilton C shell",
"Type": "Command line interface",
"Designed by": "Nicole Hamilton"
},
{
"Name": "Harbour",
"Type": "Compiled, Object-oriented class-based, Procedural, Reflective",
"Designed by": "Antonio Linares"
},
{
"Name": "Haskell",
"Type": "Compiled, Functional, Metaprogramming, Interpreted, Interactive mode",
"Designed by": "Simon Peyton Jones, Lennart Augustsson, Dave Barton, Brian Boutel, Warren Burton, Joseph Fasel, Kevin Hammond, Ralf Hinze, Paul Hudak, John Hughes, Thomas Johnsson, Mark Jones, John Launchbury, Erik Meijer, John Peterson, Alastair Reid, Colin Runciman, Philip Wadler"
},
{
"Name": "IDL",
"Type": "Array, Interactive mode",
"Designed by": "David Stern"
},
{
"Name": "J",
"Type": "Array, Interactive mode, Interpreted, Object-oriented class-based",
"Designed by": "Ken Iverson, Roger Hui"
},
{
"Name": "Java",
"Type": "Compiled, Curly-bracket, Imperative, Object-oriented class-based, Procedural, Reflective",
"Designed by": "James Gosling, Sun Microsystems"
},
{
"Name": "JavaScript",
"Type": "Curly-bracket, Interpreted, Reflective, Procedural, Scripting, Interactive mode",
"Designed by": "Brendan Eich"
},
{
"Name": "JOVIAL",
"Type": "Compiled, Procedural",
"Designed by": "System Development Corporation"
},
{
"Name": "JScript",
"Type": "Curly-bracket, Procedural, Reflective, Scripting",
"Designed by": "Microsoft"
},
{
"Name": "Julia",
"Type": "Array, Imperative, Interactive mode, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting",
"Designed by": "Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman"
},
{
"Name": "K",
"Type": "Array",
"Designed by": "Arthur Whitney"
},
{
"Name": "ksh",
"Type": "Command line interface",
"Designed by": "David Korn"
},
{
"Name": "Lasso",
"Type": "Procedural, Scripting, Object-oriented class-based",
"Designed by": "Kyle Jessup"
},
{
"Name": "Limbo",
"Type": "Curly-bracket",
"Designed by": "Sean Dorward, Phil Winterbottom, Rob Pike"
},
{
"Name": "Lisp",
"Type": "Metaprogramming, Reflective",
"Designed by": "John McCarthy"
},
{
"Name": "Lua",
"Type": "Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Object-oriented class-based, Reflective, Scripting",
"Designed by": "Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo"
},
{
"Name": "MATLAB",
"Type": "Array, Imperative, Interactive mode, Procedural",
"Designed by": "MathWorks"
},
{
"Name": "Mercury",
"Type": "Compiled, Functional",
"Designed by": "Zoltan Somogyi"
},
{
"Name": "ML",
"Type": "Compiled, Interactive mode",
"Designed by": "Robin Milner"
},
{
"Name": "Nemerle",
"Type": "Compiled, Curly-bracket, Metaprogramming, Object-oriented class-based, Procedural",
"Designed by": "Kamil Skalski, Michal Moskal, Prof. Leszek Pacholski, Pawel Olszta"
},
{
"Name": "Obix",
"Type": "Compiled, Interactive mode, Object-oriented class-based, Procedural, Reflective, Scripting",
"Designed by": "Christian Neumanns"
},
{
"Name": "Objective-C",
"Type": "Compiled, Reflective, Object-oriented class-based",
"Designed by": "Brad Cox, Tom Love"
},
{
"Name": "Pascal",
"Type": "Compiled, Imperative, Interpreted",
"Designed by": "Niklaus Wirth"
},
{
"Name": "Perl",
"Type": "Curly-bracket, Imperative, Interactive mode, Interpreted, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based",
"Designed by": "Larry Wall"
},
{
"Name": "PHP",
"Type": "Curly-bracket, Imperative, Interpreted, Object-oriented class-based, Reflective, Scripting",
"Designed by": "Rasmus Lerdorf"
},
{
"Name": "Python",
"Type": "Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based",
"Designed by": "Guido van Rossum"
},
{
"Name": "R",
"Type": "Curly-bracket, Interactive mode, Interpreted, Procedural, Scripting",
"Designed by": "Ross Ihaka, Robert Gentleman"
},
{
"Name": "REXX",
"Type": "Command line interface, Interactive mode, Interpreted, Scripting",
"Designed by": "Mike Cowlishaw"
},
{
"Name": "RPG",
"Type": "Compiled, Procedural",
"Designed by": "IBM"
},
{
"Name": "Ruby",
"Type": "Imperative, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting, Interactive mode",
"Designed by": "Yukihiro Matsumoto"
},
{
"Name": "Rust",
"Type": "Compiled, Curly-bracket, Imperative, Metaprogramming, Procedural",
"Designed by": "Graydon Hoare"
},
{
"Name": "S",
"Type": "Array",
"Designed by": "Rick Becker, Allan Wilks, John Chambers"
},
{
"Name": "S-Lang",
"Type": "Curly-bracket, Interpreted, Procedural, Scripting, Interactive mode",
"Designed by": "John E. Davis"
},
{
"Name": "Scala",
"Type": "Curly-bracket, Interactive mode, Object-oriented class-based",
"Designed by": "Martin Odersky"
},
{
"Name": "Scheme",
"Type": "Compiled, Interactive mode, Metaprogramming, Reflective",
"Designed by": "Guy L. Steele, Gerald Jay Sussman"
},
{
"Name": "Smalltalk",
"Type": "Compiled, Metaprogramming, Scripting, Interactive mode, Object-oriented class-based, Reflective",
"Designed by": "Alan Kay, Dan Ingalls, Adele Goldberg"
},
{
"Name": "SQL",
"Type": "Data-oriented, Declarative, Extension",
"Designed by": "Donald D. Chamberlin, Raymond F. Boyce"
},
{
"Name": "Swift",
"Type": "Compiled",
"Designed by": "Chris Lattner, Apple Inc."
},
{
"Name": "Turing",
"Type": "Compiled",
"Designed by": "Ric Holt, James Cordy"
},
{
"Name": "TUTOR",
"Type": "Authoring",
"Designed by": "Paul Tenczar, Richard Blomme"
},
{
"Name": "Vala",
"Type": "Compiled",
"Designed by": "Jürg Billeter, Raffaele Sandrini"
},
{
"Name": "VBScript",
"Type": "Interpreted, Procedural, Scripting, Object-oriented class-based",
"Designed by": "Microsoft"
},
{
"Name": "Visual Basic",
"Type": "Compiled, Procedural",
"Designed by": "Microsoft"
},
{
"Name": "Visual FoxPro",
"Type": "Compiled, Data-oriented, Object-oriented class-based, Procedural",
"Designed by": "Microsoft"
},
{
"Name": "Windows PowerShell",
"Type": "Command line interface, Curly-bracket, Interactive mode, Interpreted, Scripting",
"Designed by": "Microsoft"
},
{
"Name": "X#",
"Type": "Compiled, Procedural",
"Designed by": "COSMOS"
},
{
"Name": "X++",
"Type": "Compiled, Object-oriented class-based, Procedural, Reflective",
"Designed by": "Microsoft"
},
{
"Name": "X10",
"Type": "Array, Curly-bracket, Object-oriented class-based, Reflective",
"Designed by": "Kemal Ebcioglu, Vijay Saraswat, Vivek Sarkar"
},
{
"Name": "XL",
"Type": "Compiled, Procedural, Reflective, Iterative, Metaprogramming",
"Designed by": "Christophe de Dinechin"
},
{
"Name": "ZPL",
"Type": "Array",
"Designed by": "Chamberlain"
},
{
"Name": "zsh",
"Type": "Command line interface",
"Designed by": "Paul Falstad"
}
]
我有这个简单的代码片段:
#!/usr/bin/env python
import json
#load the json data file
data = json.loads(open("data.json").read())
#get the Type from a prompt
typeStr = raw_input("Type: ")
flag = False;
for lang in data:
if typeStr == lang['Type']:
print "Name: " + lang['Name']
print "Designed by: " + lang['Designed by'] + "\n"
flag = True
if flag == False:
print "Not found."
else:
print "Done"
然后输出:
Type: Scripting
Name: AppleScript
Designed by: Apple Inc.
Done
它应遍历所有具有Type ==" Scripting"的对象。但我不确定当它获得1个值时,它会退出循环。
答案 0 :(得分:3)
您只是在寻找完全匹配。其他Type
条目包含逗号和更多文本:
"Type": "Curly-bracket, Scripting",
"Type": "Command line interface, Scripting",
等。 你必须拆分这些字符串:
for lang in data:
if typeStr in (t.strip() for t in lang['Type'].split(',')):
print "Name: " + lang['Name']
print "Designed by: " + lang['Designed by'] + "\n"
flag = True
这匹配精确类型,以逗号分隔,仍然;您找不到'Curly'
(子字符串)的任何匹配项,仅适用于'Curly-bracket'
。
如果子串匹配正常,请使用in
而不拆分:
if typeStr in lang['Type']:
甚至输入一个字母也会产生匹配。
答案 1 :(得分:2)
你可能想要像
这样的东西 if typeStr in lang['Type']:
答案 2 :(得分:1)
将if typeStr == lang['Type']:
更改为if typeStr in lang['Type']:
。您只有一个条目只包含脚本。其他人把它混合成一个更长的字符串。