繁体   English   中英

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

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

这是我要完成的工作:输入与定义的词典中的键相对应的项的文本文件列表,让程序打印该列表中所有键的所有值。 如您所见,字典设置为每个键包含多个值。 字典报告单个键的值很好,但是我在尝试使其报告多个键(在文本文件列表中定义)的值时遇到问题。

这是代码:

# 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')

尝试运行它,它返回:

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']"

KeyError消息中列出的项目是lookuplist文本文件中的项目。 我的代码是否将整个lookuplist文件视为查找的关键? 我怎么能把这个文件中的每一行都当作查找键呢?

谢谢!

编辑/更新:

使用[0],[1]等指定每个要查找的键,然后写入输出文件的方法相当不雅。 这是我使用的最终代码:

# 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

想通了,我想为其他想要做同样事情的人更新。

我想如果你改变

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

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

你应该靠近一点

但是,我认为您将要一次从查找列表中查找项目,因为查找是一个列表

再次查看您的代码,尽管我不太确定您在做什么,但我做了一些其他更改,但类似

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

我在这里确实感觉到了自己的方式,但是根据您所提出的问题,这与我所能达到的结果非常接近。 我找不到对“ Alt名称”的引用,并且您的代码没有指示如何访问字典中的该键,因此我认为它是来自最初读取的文件中的值。

暂无
暂无

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

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