Reputation: 23
I wanted to open a pdb file and extract its sequence but list.append() does not add any object to my list and displays[ ] as output.I tried doing all the possible alternatives but this doesnot work at atall.
file=open("c:/pdb/1ana.pdb")
for lines in file:
list1=lines.split()
id=list1[0]
list=[]
if id=='ATOM':
if list1[2]=='C4':
list.append(list1[3])
print(list)
Upvotes: 1
Views: 892
Reputation: 2523
list=[]
is inside for loop which will reset the list
in each iteration.
Also, avoid using list
as a variable name because list
is a keyword in python
Upvotes: 5
Reputation: 938
It is important to understand how the logic of a loop works. Since you have declared an empty list inside a for loop, each time the iterator (lines in your case) iterates over lines, it resets your appended list to an empty list.
So to avoid this, declare your list variable outside of the for loop and append inside your loop.
file=open("c:/pdb/1ana.pdb")
list = []
for line in file:
list1=lines.split()
id=list1[0]
if id=='ATOM':
if list1[2]=='C4':
list.append(list1[3])
print(list)
Cheers.
Upvotes: 0