简体   繁体   中英

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. Is my code treating the entire lookuplist file as a key for looking up? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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