-3

I have a txt file (dictionary.txt) that I would like to load into my script as a dictionary. How can I do so?

file dictionary.txt

{'YAL008W': 25, 'YBR255W': 50, 'YGR164W': 37, 'YGR131W': 40, 'YNL003C': 11,
'YBR135W': 2, 'YBR160W': 6, 'YJL082W': 79, 'YJL142C': 4, 'YPL191C': 38,
 'YGL215W': 31, 'YKL074C': 33, 'YJL077C': 67, 'YKL096W-A': 22, 'YIL124W': 60,
 'YLR364C-A': 2, 'YPL039W': 58, 'YNL170W': 16, 'YGL141W': 62, 'YJL179W': 15,
 'YDR316W-A': 13, 'YDR316W-B': 139, 'YKL083W': 25, 'YOR009W': 25,
 'YKL029C': 395, 'YPL166W': 31, 'YKL052C': 20, 'YOL034W': 29, 'YBL008W': 42,
 'YIL062C': 2, 'YCL023C': 27}

Code:

f=open('dictionary.txt', 'r') 

with f as dic1:
     dictionary1=eval(dic1.read())   

This inputs it as a dictionary, but I was wondering if there is a faster way to do so.

Jasper
  • 3,939
  • 1
  • 18
  • 35
user5927494
  • 129
  • 1
  • 10
  • Please submit some code that shows us you tried to do something at least – Seekheart Feb 23 '16 at 23:41
  • I just edited my question. hope that helps – user5927494 Feb 23 '16 at 23:43
  • is this data in a known format? For example, is it supposed to be valid JSON? How was the data created? – Bryan Oakley Feb 23 '16 at 23:45
  • Possible duplicate of [Converting a String to Dictionary?](http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary) – Jasper Feb 23 '16 at 23:47
  • I created the data myself. The keys are gene names and values are integer count of reads that mapped to the gene. I created an empty dictionary, saved all gene names into keys and then had a for loop run through and add appropriate number of times a read was mapped to the gene. The dictionary was saved as a .txt file with sys.stdout @BryanOakley – user5927494 Feb 23 '16 at 23:47
  • Side-note: It's extraordinarily weird and partially defeating the purpose of using `with` statements to separate the `open` line from the `with`. You'd almost always combine them into `with open('dictionary.txt') as dic1:` (I omitted the `'r'` since that's the default mode anyway). – ShadowRanger Feb 23 '16 at 23:49
  • That data is not split up like you have it now, that's only for posting purpose, correct? – Leb Feb 23 '16 at 23:59
  • No, it is split it up that way @Leb – user5927494 Feb 24 '16 at 00:48

2 Answers2

3

It's unlikely to be faster, or slower, but you can make it much more secure by avoiding eval (which can execute arbitrary code, which makes for a major security and stability risk) in favor of ast.literal_eval; it's like eval, but only for Python literals, not arbitrary code. Depending on format, you might also be able to use json.load or json.loads.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

Your dictionary.txt is almost JSON, so I would recommend something like this:

import json
with open('dictionary.txt') as data_file:
    data = data_file.read()
# Convert single quotes to double quotes to get JSON syntax
data = data.replace('\'', '"')
data = json.loads(data)
for key, val in data.iteritems():
    print "%s = %s" % (key, val)

For your dictionary.txt, this outputs

YOL034W = 29
YAL008W = 25
...
YKL052C = 20

NOTE: Ellipses have been used to abbreviate the output.