Hi I am VERY new to python. I am trying to write a function that returns lines from a text file to a new file. I can't seem to get it right. So far my code looks like this:

def seperate_atoms(f):#Enter .pdb file in with quotations
	myfile=open(f,'r')
	crudepdb=myfile.readlines()
	cleanpdb=[]
	for line in crudepdb:
		templine=line.strip()
		cleanpdb.append(templine)
	for x in cleanpdb:
		if x[0:4]=="ATOM":
			x=("myfile","w")
			print>>x

Does anyone know what I'm doing wrong?

Recommended Answers

All 5 Replies

you are trying to append line to file x, but the x is set to tuple ("myfile","w") at previous line. How could it work?

Do you mean to do something like:

import sys
def seperate_atoms(f):#Enter .pdb file in with quotations

    with open(f,'r') as myfile:
        crudepdb= [line.strip()
                   for line in myfile
                   if line.strip().startswith("ATOM")]

    with open("myfile.txt","w") as x:
        x.write('\n'.join(crudepdb))

you are trying to append line to file x, but the x is set to tuple ("myfile","w") at previous line. How could it work?

Do you mean to do something like:

import sys
def seperate_atoms(f):#Enter .pdb file in with quotations

    with open(f,'r') as myfile:
        crudepdb= [line.strip()
                   for line in myfile
                   if line.strip().startswith("ATOM")]

    with open("myfile.txt","w") as x:
        x.write('\n'.join(crudepdb))

Thank you for your help... but then the the lines x are being modified and I need to keep them in the same form. I'm just trying to parse the original file and dump them into new files. In my code right now I am just trying to figure out the syntax.

When I just print x, it spits out exactly what I want.

def seperate_atoms(f): #Enter .pdb file in with quotations
	myfile=open(f,'r')
	crudepdb=myfile.readlines()
	cleanpdb=[]
	for line in crudepdb: #Cleans up the original .pdb file
		templine=line.strip()
		cleanpdb.append(templine)
	for x in cleanpdb: #Parse based on first four letters in .pdb file
		if x[0:4]=="ATOM":
			print x

Write takes string as argument,if x is a string is shall be ok.
If not ''.join() as tony used be an option.
Remeber to close() the file,if not it will not write anything.
with open() close the file auto,as tony use over.

if x[0:4]=="ATOM":
   print type(x) #Check the type

>>> help(f.write)
Help on built-in function write:

write(...)
    write(str) -> None.  Write string str to file.
    
    Note that due to buffering, flush() or close() may be needed before
    the file on disk reflects the data written.

Write takes string as argument,if x is a string is shall be ok.
If not ''.join() as tony used be an option.
Remeber to close() the file,if not it will not write anything.
with open() close the file auto,as tony use over.

if x[0:4]=="ATOM":
   print type(x) #Check the type

>>> help(f.write)
Help on built-in function write:

write(...)
    write(str) -> None.  Write string str to file.
    
    Note that due to buffering, flush() or close() may be needed before
    the file on disk reflects the data written.

Thank you! I got it to create a file with all of the x strings. My problem now is that they are listed one right after another (ie x x x x ...) and I would rather have them listed as
x
x
x
x
.
.
.

Do you know how to change that formatting?

Add '\n' in each write.

Cheers and Happy coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.