简体   繁体   English

从字典中获取值

[英]get the value from dictionary

I have a text file and it includes dictionaries, I want to get the value for all the Swiss-Prot and print them in separate lines.我有一个文本文件,其中包含字典,我想获取所有 Swiss-Prot 的值并将它们打印在单独的行中。

For example, I want them to be like this as two column of query and swiss-prot例如,我希望它们像这样作为查询和 swiss-prot 的两列

Query  Swiss-Prot

MED13  Q9UHV7

NIPBL  Q6KC79

This is a short example of the text type:这是文本类型的一个简短示例:

[{"query": "MED13", "_id": "9969", "_score": 89.814735, "uniprot": {"Swiss-Prot": "Q9UHV7", "TrEMBL": ["J3QRB7", "A0A024QZ75"]}}, {"query": "NIPBL", "_id": "25836", "_score": 90.15312, "uniprot": {"Swiss-Prot": "Q6KC79", "TrEMBL": ["H0Y8M3", "A0A590UJS4"]}}, {"query": "XPO1", "_id": "7514", "_score": 90.00326, "uniprot": {"Swiss-Prot": "O14980", "TrEMBL": ["A0A7I2V461", "A0A7I2V2Y6", "A0A7I2V6B9", "B3KWD0", "A0A7I2V396", "A0A7I2V531", "C9J673", "A0A7I2V3P3", "A0A7I2V3J1", "A0A7I2V2H0", "A0A7I2YQV4", "C9IYM2", "A0A7I2YQP1", "A0A7I2V3N0", "A0A7I2V2S3", "C9JKM9", "A0A7I2V3W6", "A0A7P0Z4B7", "A0A7I2V2C5", "A0A7I2V4A3", "F8WF71", "A0A7I2V488", "A0A7I2YQX3"]}}, {"query": "PRR12", "_id": "57479", "_score": 93.08752, "uniprot": {"Swiss-Prot": "Q9ULL5"}}, {"query": "USP9X", "_id": "8239", "_score": 91.546684, "uniprot": {"Swiss-Prot": "Q93008", "TrEMBL": ["Q86X58", "Q6P468"]}}, {"query": "POLA1", "_id": "5422", "_score": 87.691986, "uniprot": {"Swiss-Prot": "P09884", "TrEMBL": ["A0A5F9ZHW2", "A6NMQ1", "A0A7I2V2V3"]}}, {"query": "SYNGAP1", "_id": "8831", "_score": 92.250496, "uniprot": {"Swiss-Prot": "Q96PV0", "TrEMBL": ["B7ZCA0", "A0A1U9X8L0", "A0A2R8YDS2", "A0A1B0GW70", "A0A140T8W4", "A0A0A0MQZ2", "A0A2R8Y6T2", "A0A804HJ92"]}}, {"query": "PRKDC", "_id": "5591", "_score": 87.73935, "uniprot": {"Swiss-Prot": "P78527", "TrEMBL": ["F5GX40", "H0YG84"]}}, {"query": "HDAC4", "_id": "9759", "_score": 87.96423, "uniprot": {"Swiss-Prot": "P56524", "TrEMBL": ["H7C397", "A0A7I2SVS4", "C9J0X4", "H7BZT3", "C9J481"]}}, {"query": "SMG1", "_id": "23049", "_score": 87.58261, "uniprot": {"Swiss-Prot": "Q96Q15", "TrEMBL": ["H3BR09", "I3L400", "J3KRA9", "H3BPS6", "A0A087X1K8", "I3L0W2", "E9PNP6", "I3L144", "H3BQN7"]}}, {"query": "ZC3H4", "_id": "23211", "_score": 88.41728, "uniprot": {"Swiss-Prot": "Q9UPT8", "TrEMBL": "M0QY97"}}, {"query": "COL5A1", "_id": "1289", "_score": 88.062645, "uniprot": {"Swiss-Prot": "P20908", "TrEMBL": ["B2ZZ86", "A0A024R8E5", "Q59EE7", "H7BY82"]}}, {"query": "SMARCA4", "_id": "6597", "_score": 91.729965, "uniprot": {"Swiss-Prot": "P51532", "TrEMBL": ["A7E2E1", "Q9HBD4", "B3KNW7", "A0A2R8Y7S2", "A0A2R8YF80", "A0A2R8Y523", "A0A2R8Y6N0", "A0A2R8Y583", "A0A2R8Y7Y7", "A0A2R8Y7R0", "A0A2R8Y440", "A0A2R8YGP5", "A0A2R8Y866", "A0A2R8YFK5", "A0A2R8YDA1", "A0A2R8YF38", "K7EP28", "A0A2R8Y526", "A0A2R8Y5K3", "A0A2R8YFV8", "A0A2R8YF58", "A0A2R8YGG3", "A0A2R8Y7F3", "K7EQF0", "A0A2R8YG32", "A0A2R8Y4P4", "A0A2R8Y4C5", "A0A2R8YCY3", "A0A2R8Y6V2", "A0A2R8Y4R6"]}}, {"query": "TNPO1", "_id": "3842", "_score": 87.91549, "uniprot": {"Swiss-Prot": "Q92973", "TrEMBL": ["E7EW37", "A0A024RAM0", "A0A7P0T9C0", "S4R398", "A0A7P0T944", "A0A7P0TBF0", "A0A7P0Z4D3"]}}, {"query": "AGO1", "_id": "26523", "_score": 91.19263, "uniprot": {"Swiss-Prot": "Q9UL18", "TrEMBL": ["B2RAD8", "Q5TA58", "A0A6I8PTZ8", "B3KME0", "A0A0U1RQZ8"]}}, {"query": "ARHGAP35", "_id": "2909", "_score": 90.83041, "uniprot": {"Swiss-Prot": "Q9NRY4"}}, {"query": "LRP1", "_id": "4035", "_score": 90.87773, "uniprot": {"Swiss-Prot": "Q07954", "TrEMBL": ["Q6PJ72", "Q59FG2", "Q7Z7K9", "H0YJ88", "H0YJI8"]}}, {"query": "TOP1", "_id": "7150", "_score": 88.75276, "uniprot": {"Swiss-Prot": "P11387", "TrEMBL": ["A0A7P0T852", "Q9BVT2", "A0A7P0T9R7"]}}, {"query": "TRIP12", "_id": "9320", "_score": 87.6137, "uniprot": {"Swiss-Prot": "Q14669", "TrEMBL": ["A0A6Q8PGG9", "A0A6Q8PHK0", "G5E9G6", "C9JSX9", "C9JLJ5", "C9JLD7", "H7C1L9", "H7C2Y1", "F8W9P3"]}}, {"query": "KMT2E", "_id": "55904", "_score": 87.338264, "uniprot": {"Swiss-Prot": "Q8IZD2", "TrEMBL": ["C9JNE1", "A0A087WYW5", "S4R363", "C9JQ68", "H7C5C4", "A0A590UK87", "H7C4F3", "C9JYI9", "F8WAU9"]}}, {"query": "HCFC1", "_id": "3054", "_score": 92.59821, "uniprot": {"Swiss-Prot": "P51610", "TrEMBL": ["H7C1C4", "A6NEM2"]}}, {"query": "UBTF", "_id": "7343", "_score": 88.12895, "uniprot": {"Swiss-Prot": "P17480", "TrEMBL": ["E9PLT2", "E9PKP7", "H0YDH7", "E9PMM2"]}}, {"query": "HUWE1", "_id": "10075", "_score": 88.91688, "uniprot": {"Swiss-Prot": "Q7Z6Z7", "TrEMBL": ["A0A1B0GXC7", "A0A024R9W5", "H0Y659", "A0A087X146", "A0A087X1S3"]}}, {"query": "KDM3B", "_id": "51780", "_score": 87.94795, "uniprot": {"Swiss-Prot": "Q7LBC6", "TrEMBL": ["H0Y946", "H0Y9V5"]}}, {"query": "TRRAP", "_id": "8295", "_score": 90.83041, "uniprot": {"Swiss-Prot": "Q9Y4A5", "TrEMBL": ["A0A2R8YFJ4", "F2Z2U4", "C9K0N1", "H0Y4W2"]}}, {"query": "GRIN2B", "_id": "2904", "_score": 89.35281, "uniprot": {"Swiss-Prot": "Q13224", "TrEMBL": ["A0A1B0GU78", "A0A0D9SFK0", "A0A0D9SFA0"]}}, {"query": "USP7", "_id": "7874", "_score": 87.70774, "uniprot": {"Swiss-Prot": "Q93009", "TrEMBL": ["B7ZAX6", "B7Z855", "Q6U8A4", "I3L2D8", "H3BVA7", "H3BQD1", "H3BRI4", "F5H2X1", "H3BTM1", "H3BND8", "H3BMF6", "H3BRA2", "H3BUV0", "A0A669KBL1"]}}, {"query": "ATP1A3", "_id": "478", "_score": 93.2793, "uniprot": {"Swiss-Prot": "P13637", "TrEMBL": ["M0R116", "Q53ES0", "A0A0A0MT26", "M0QXF2"]}}, {"query": "ASH1L", "_id": "55870", "_score": 87.80293, "uniprot": {"Swiss-Prot": "Q9NR48", "TrEMBL": ["A0A7I2YQU9", "A0A7I2V316", "A0A7I2V3D6", "H0YI82", "A0A7I2V4K0", "A0A7I2YQL4", "F8VWK7", "A0A7I2V542", "A0A7I2V4H9"]}}, {"query": "ANKRD17", "_id": "26057", "_score": 90.394646, "uniprot": {"Swiss-Prot": "O75179", "TrEMBL": ["H0YLQ3", "H0YM23"]}}, {"query": "SMC1A", "_id": "8243", "_score": 89.55028, "uniprot": {"Swiss-Prot": "Q14683", "TrEMBL": ["G8JLG1", "A0A384MR33", "Q68EN4", "A0A6Q8PHC3", "V9GY57", "V9GYN9"]}}]

It looks like you have a json-formatted file.看起来你有一个 json 格式的文件。 You can read this using the json module , and process the resulting list of dictionaries to print the output you want.您可以使用json模块阅读此内容,并处理结果字典列表以打印您想要的 output。

import json

with open("input.txt") as f:
    data = json.load(f)

print("Query\tSwiss-Prot")
for item in data:
    q = item["query"]
    sp = item["uniprot"]["Swiss-Prot"]
    print(f"{q}\t{sp}")

Which gives:这使:

Query   Swiss-Prot
MED13   Q9UHV7
NIPBL   Q6KC79

If you have multiple lines in your input file which are all separately json-formatted, you could use the following to parse your file.如果您的输入文件中有多行都是单独的 json 格式,您可以使用以下内容来解析您的文件。 Here, I read each line from the file, parse the json, and add all the elements of the resulting list to the data list.在这里,我从文件中读取每一行,解析 json,并将结果列表的所有元素添加到data列表中。 The code to print the output remains the same.打印 output 的代码保持不变。

import json

data = []
with open("input.txt") as f:
    for line in f:
        data.extend(json.loads(line))        

Your datas are in a list with a dictionnary for each entry.您的数据在一个列表中,每个条目都有一个字典。 Iterate with a loop over entries and extract values.循环遍历条目并提取值。

print("Query\tSwiss-Prot")

for entry in datas:
    if 'query' in entry:
        if ('uniprot' in entry) and ('Swiss-Prot' in entry['uniprot']):
            print(f"{entry['query']}\t{entry['uniprot']['Swiss-Prot']}")
        else:
            print(f"{entry['query']}\tUnknown")
# import json for converting your text file
import json

# open your text file
with open("yourfile.txt", "r") as file:
    data = file.read()

# convert your data to json (dictionary)
convert_to_dict = json.loads(data)

# loop through the dict file and print the results
for i in convert_to_dict:
    print(i["query"], " ", i["uniprot"]["Swiss-Prot"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM