I have a database from which I am trying to extract certain information.
The logical structure goes like this:
gc; QUERY
ft; NAME (need this field)
fd; SEQUENCE (need this field)
fd; SEQUENCE
… (more “fd;” fields)
ft; NAME (need this field)
fd; SEQUENCE (need only first fd)
… (more fd; fields)
ft; NAME (need this field)
fd; SEQUENCE (need only first fd)
gc; ANOTHER QUERY
This structure is repeated thousands of times, one for each QUERY. I have parenthisized the fields I need to extract. I tried the following code, both as a function and as a snippet but it isn’t working. I have checked that the variable scopes are correct.
I need mylist to contain the same number of entries as there are “ft” fields.
With my code, mylist is created but is an empty list. Any ideas where my logic is wrong? I am using python 2.6.5
mylist = []
query = raw_input("query: ")
flag = 0
for line in lines:
if line.startswith('gc; ' + query):
flag == 1
continue
elif line.startswith('fc; ') and flag == 1:
print line
flag = 1
elif line.startswith('fd; ') and flag == 1:
print line
mylist.append(line)
flag = 0
elif line.startswith('fd; ') and flag == 0:
continue
else:
continue
In the 6th line of code, flag == 1 should only have 1 equals sign. Otherwise, it’s never being set to 1, which then fails your other checks. Additionally, unless there’s more code below that which is cut off off, you don’t need any of those continues – it will automatically go to the next iteration of the loop.