You want Frog virus 3 (isolate Goorha) (FV-3) to be the key in your dictionary and the rest the value?
Assuming that and assuming that your file is not too big (and as such can be comfortably kept in memory) I have some code you could try...
import sys
def makedict(datalist):
data = ' '.join(datalist),replace('[','%@%@').replace(']', '%@%@)
info = data.split('%@%@')
return((info[1], [info[0], info[2]]))
with open(sys.argv[1]) as input:
outdict = {}
content = [line.strip() for line in input.readlines() if not line == ""]
tempdata = []
for line in content:
if line.startswith('>'):
if not len(tempdata) = 0:
key, value = makedict(tempdata)
outdict[key] = value
else:
tempdata = [line,]
else:
tempdata.append(line)
else:
key. value = makedict(tempdata)
outdict[key] = value
Notice:
-the list comprehension to properly format the input
-the else clause on the for loop to also convert the last entry
-storing the objects in the templist and emptying this after creating a dict key and value based on it
-in makedict function I replace brackets by something we do not expect in the file at all, which I use subsequently for splitting allowing me to isolate the species name.
- the function makedict returns a tuple with first the key and second element the rest of the info
Since I do not have your (complete) inputfile I haven't been able to test it, so let me know if something goes wrong which you can't fix.
How can I post code without this messed up layout?
Above the text box you write in, there should be a number of box icons which you can use to edit the text... Highlight your code and then click the little box with the ones and zeroes in it.
Thanks! It works.