简体   繁体   English

文本文件列表作为在Python中的字典中查找的键

[英]Text file list as keys to lookup in dictionary in Python

Here's what I'm trying to accomplish: input a text file list of items that correspond to keys within a defined dictionary, have the program print all values for all keys within that list. 这是我要完成的工作:输入与定义的词典中的键相对应的项的文本文件列表,让程序打印该列表中所有键的所有值。 As you can see, the dictionary is set up to contain multiple values for each key. 如您所见,字典设置为每个键包含多个值。 The dictionary reports values for individual keys just fine, but I'm having problems trying to get it to report values for multiple keys (defined within the text file list). 字典报告单个键的值很好,但是我在尝试使其报告多个键(在文本文件列表中定义)的值时遇到问题。

Here's the code: 这是代码:

# open the text file containing a list that will be looked up in the dictionary below 
with open('/home/matt/Source/python/fpkm-batch-lookup/lookuplist.txt', 'r') as lookupfile:
    lookup = str(lookupfile.read().splitlines())

# define output file
output = open('/home/matt/Source/python/fpkm-batch-lookup/output.csv', 'w')

# assign keydict dictionary keys and values as columns in input file
keydict = {}
data = open('/home/matt/Source/python/fpkm-lookup/input.csv', 'r')
for line in data:
    items = line.rstrip('\n').split(',')
    key, values = items[0], items[1:]
    keydict[key] = values

# write out key and values to output file
output.write(lookup + '\n' + str(keydict['Alt Name']) + '\n' + str(keydict[lookup]) + '\n')

Trying to run it, it returns: 尝试运行它,它返回:

Traceback (most recent call last):
  File "/home/matt/Source/python/fpkm-batch-lookup/run.py", line 17, in <module>
    output.write(lookup + '\n' + str(keydict['Alt Name']) + '\n' + str(keydict[lookup]) + '\n')
KeyError: "['ENSG00000196476_C20orf96', 'ENSG00000247315_ZCCHC3', 'ENSG00000225377_RP5-1103G7.4', 'ENSG00000177732_SOX12', 'ENSG00000101255_TRIB3']"

The listed items in the KeyError message are the items in the lookuplist text file. KeyError消息中列出的项目是lookuplist文本文件中的项目。 Is my code treating the entire lookuplist file as a key for looking up? 我的代码是否将整个lookuplist文件视为查找的关键? How could I have it treat each line within this file as a key to look up instead? 我怎么能把这个文件中的每一行都当作查找键呢?

Thanks! 谢谢!

Edit/Update: 编辑/更新:

The rather inelegant approach of using [0], [1], etc to designate each key to lookup and then write to the output file. 使用[0],[1]等指定每个要查找的键,然后写入输出文件的方法相当不雅。 Here's the final code I used: 这是我使用的最终代码:

# open the text file containing a list that will be looked up in the dictionary below 
lookuplist = open('/home/matt/Source/python/fpkm-batch-lookup/lookuplist.txt').read().splitlines()

# define output file
output = open('/home/matt/Source/python/fpkm-batch-lookup/output.csv', 'w')

# assign keydict dictionary keys and values as columns in input file
keydict = {}
data = open('/home/matt/Source/python/fpkm-lookup/input.csv', 'r')
for line in data:
    items = line.rstrip('\n').split(',')
    key, values = items[0], items[1:]
    keydict[key] = values

# write out key and values to output file - add more if needed

output.write(str(lookuplist[0]) + '\n' + str(keydict['Alt Name']) + '\n' + str(keydict[lookuplist[0]]) + '\n' + '\n')

output.write(str(lookuplist[1]) + '\n' + str(keydict['Alt Name']) + '\n' + str(keydict[lookuplist[1]]) + '\n' + '\n')

output.write(str(lookuplist[2]) + '\n' + str(keydict['Alt Name']) + '\n' + str(keydict[lookuplist[2]]) + '\n' + '\n')

# .... to the nth string number to lookup

Figured I'd update, for anyone else looking to do the same sort of thing. 想通了,我想为其他想要做同样事情的人更新。

I think if you change 我想如果你改变

with open('/home/matt/Source/python/fpkm-batch-lookup/lookuplist.txt', 'r') as lookupfile:
    lookup = str(lookupfile.read().splitlines())

to

with open('/home/matt/Source/python/fpkm-batch-lookup/lookuplist.txt', 'r') as lookupfile:
    lookup = lookupfile.read().splitlines()

You should be closer 你应该靠近一点

However, I think you are going to want to lookup the items from the lookup list one at a time as lookup is a list 但是,我认为您将要一次从查找列表中查找项目,因为查找是一个列表

looking at your code again I have made some more changes though I am not totally sure what you are doing but something like 再次查看您的代码,尽管我不太确定您在做什么,但我做了一些其他更改,但类似

for key in lookup:
    if key in keydict:
         output.write(key + '\n' + ','.join(keydict[key]) + '\n' +key +'\n')

I am really feeling my way here but this is as close as I can get to your output based on what you have in your question. 我在这里确实感觉到了自己的方式,但是根据您所提出的问题,这与我所能达到的结果非常接近。 I can't find a reference to 'Alt Name' and your code gives no indication of how to access that key in the dictionary so I assume it is the value from the file read in originally. 我找不到对“ Alt名称”的引用,并且您的代码没有指示如何访问字典中的该键,因此我认为它是来自最初读取的文件中的值。

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

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