【问题标题】:Text file list as keys to lookup in dictionary in Python文本文件列表作为在 Python 中查找字典的键
【发布时间】:2016-04-05 15:03:53
【问题描述】:

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

代码如下:

# 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 消息中列出的项目是查找列表文本文件中的项目。我的代码是否将整个查找列表文件视为查找的关键?我怎么能让它把这个文件中的每一行都当作一个键来查找呢?

谢谢!

编辑/更新:

使用 [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

我想我会更新,给其他想做同样事情的人。

【问题讨论】:

  • 首先观察,从文件中读取的内容将是一个字符串,因此您不必将其字符串化 str(lookupfile.read().splitlines())
  • 当我看到“['ENSG00000196476_C20orf96', 注意双引号
  • 从查找中删除 str() = 返回一个 TypeError: can only concatenate list (not "str") to list ... 所以这就是我将 str() 扔进去的原因。
  • 引用的是什么行号
  • 我很确定这是因为您没有一次引用查找中的项目,请查看下面的代码

标签: python list dictionary key


【解决方案1】:

我想如果你改变

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 Name”的引用,并且您的代码没有说明如何访问字典中的该键,所以我认为它是最初读入的文件中的值。

【讨论】:

  • 从查找中删除 str() = 返回一个 TypeError: can only concatenate list (not "str") to list
  • 对,但请注意我在查找循环中的 for 键 - 查找应该是一个键吗?
  • 您将遇到另一个问题,因为根据我所看到的字典中没有“Alt Name”
  • 我同意你应该仔细看看这个答案。拆分行后,您将获得一个键列表,每行一个。您当然不想将这些项目连接在一起。
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 2014-03-14
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多