在Python中从打印输出中删除用户输入

时间:2019-02-05 04:23:06

标签: python json python-2.7

我有一个程序,要求用户输入名称(例如,里斯本),而属于该用户名的其他用户的国家/地区与要打印的用户输入(例如,里斯本)的国家/地区相同(例如,玉,约翰)。这是我的JSON文件:

{  
   "user1":{  
      "Country":[  
         "China",
         "USA",
         "Nepal"
      ],
      "Name":[  
         "Lisbon"
      ]
   },
   "user2":{  
      "Country":[  
         "Sweden",
         "China",
         "USA"
      ],
      "Name":[  
         "Jade"
      ]
   },
   "user3":{  
      "Country":[  
         "India",
         "China",
         "USA"
      ],
      "Name":[  
         "John"
      ]
   }
}

例如,如果用户输入为Lisbon,则将为输出:

Lisbon
Jade
John

我想从输出中删除用户输入的内容,以使预期结果为:

Jade
John

这是我的代码:

def Country():
    userName = raw_input("Enter user's name: ")
    with open('listOfUsers.json') as f:
        data = json.load(f)

    def getId(name):
        for userId, v in data.items():
            if v['Name'][0] == name:
                return userId;

    id = getId(userName)
    for k, v in data.items():
    if any(x in data[id]['Country'] for x in v['Country']):
        print (v['Name'][0])

1 个答案:

答案 0 :(得分:2)

以下内容对我有用

def Country():
    userName = raw_input("Enter user's name: ")
    with open('listOfUsers.json') as f:
        data = json.load(f)

    def getId(name):
        for userId, v in data.items():
            if v['Name'][0] == name:
                return userId;

    id = getId(userName)
    for k, v in data.items():
        if any(x in data[id]['Country'] for x in v['Country']):
            if v['Name'][0] != userName:
                print (v['Name'][0])