# open file for writing f = open('dnafile.txt', 'w') # prepare the file with some test data for x in range(1, 10): f.write("Name {0}\n".format(x)) f.write("Sequence {0} GTCAGTCAGTCA\n".format(x)) # close the file f.close() # This is the main good part: # open the file for reading f = open("dnafile.txt", 'r') # create a dictionary to store the values dna = dict() # prime the pump by loading the first line line = f.readline() # loop if line has a value while line: # create a dict with the first line as a key and the next line as the value # strip() removes the newline from the end of each string dna[line.strip()] = f.readline().strip() # ready the next line for the line = f.readline() # close the file f.close() # loop through all of the sequences for seq in dna: print "{0} is {1}".format(seq, dna[seq]) # print a specific sequence print "The sequence with name 'Name 9' is {0}".format(dna["Name 9"]);
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question