在json文件中搜索字符串

时间:2016-06-05 13:19:13

标签: python json list dictionary

我有一个用户在开头输入值的字典,然后这些值以json格式存储到txt文件中。目标是让用户能够使用他们选择的字符串搜索json文件。我尝试了各种各样的方法,但却出现了不成功的问题。下面的代码显示用户是否输入“s”,然后系统会提示他们输入搜索词。然后代码加载json文件并将文件中的信息存储到名为“data”的“列表”中,然后代码尝试搜索列表中的每个项目并将其与用户输入的searchTerm进行比较

将用户输入放入txt文件(json格式)的代码

if choice == 'a':
    # Add a new joke.
    # See Point 3 of the "Requirements of admin.py" section of the assignment brief.
    jokeSetup = input('Enter setup of joke: ')
    jokePunchLine = input('Enter puncline of joke: ')
    entry = {'setup': jokeSetup , 'punchline': jokePunchLine}
    data.append(entry)
    file = open('data.txt', 'w')
    json.dump(data, file)
    file.close()
    print('Joke Added.')
    pass


elif choice == 's':
    # Search the current jokes.
    # See Point 5 of the "Requirements of admin.py" section of the assignment brief.
    searchTerm = input('Enter search term: ')
    file = open('data.txt', 'r')
    data = json.load(file)
    file.close()

    for item in data:
        if searchTerm in data:
            print ('found it')


    pass

2 个答案:

答案 0 :(得分:1)

你可以这样做:

if choice == 'a':
# Add a new joke.
# See Point 3 of the "Requirements of admin.py" section of the assignment brief.
    jokeSetup = input('Enter setup of joke: ')
    jokePunchLine = input('Enter punchline of joke: ')
    entry = {'setup': jokeSetup , 'punchline': jokePunchLine}
    data.append(entry)
    file = open('data.txt', 'w')
    json.dump(data, file)
    file.close()
    print('Joke Added.')
    pass

elif choice == 's':
    # Search the current jokes.
    # See Point 5 of the "Requirements of admin.py" section of the assignment brief.
    searchTerm = input('Enter search term: ')
    file = open('data.txt', 'r')
    data = json.load(file)
    file.close()

    for item in data[0].items():
        if searchTerm in str(item[1]):
            print ('found it')

    pass

# or the for loop could be like this
for sub_dict in data:
    if searchTerm in sub_dict['setup'] or searchTerm in sub_dict['punchline']:
        print('found!')

答案 1 :(得分:1)

import json
import sys
import os
data = []
if os.stat("data.txt").st_size != 0 :
    file = open('data.txt', 'r')
    data = json.load(file)
    print(data)

choice = input("What's your choice ?")
if choice == 'a':
    # Add a new joke.
    # See Point 3 of the "Requirements of admin.py" section of the assignment brief.
    jokeSetup = input('Enter setup of joke: ')
    jokePunchLine = input('Enter punchline of joke: ')
    entry = {'setup': jokeSetup , 'punchline': jokePunchLine}
    data.append(entry)
    file = open('data.txt', 'w')
    json.dump(data, file)
    file.close()
    print('Joke Added.')
    pass


elif choice == 's':
    # Search the current jokes.
    # See Point 5 of the "Requirements of admin.py" section of the assignment brief.
    searchTerm = input('Enter search term: ')
    file = open('data.txt', 'r')
    data = json.load(file)
    file.close()

    for sub_dict in data:
        if searchTerm in sub_dict['setup']:
            print(sub_dict['punchline'])
    pass
# or you could modify the last for loop, like this:
    for dict in data:
        if searchTerm in dict['setup'] or searchTerm in dict['punchline']:
            print('found!')
    pass