python - Reorder lines in a file -
I would like to know there is a way to reorder rows in a file:
I Some atomic coordinates, one file with each atom in one line, and I need to write some molecules before others:
Atomic C1 atom C2 atom C3
I need to rearrange the lines something like this:
Atomic C2 builds C1Aat C3
Is there any way to do this without the list?
Even a list made, I did not succeed, the last test was:
i = open ("input.pdb", "r") o = For line in Open ("output.pdb", "w") l = [] I: Attach line (line split ()) to line in line: if in the "atom c2" row: a = l Index (line) b = int (a) -1 l [a] = l [b] for line in l: 0.write ("{} \ n" .format (line)) o.close () os. Remove ("input.pdb")
Any ideas? Suppose, because you do not give any other indication, you already know in which order lines should be written.
"post-text" itemprop = "text">
/ P>
atom c1 # line 0 atom c2 # line 1 atom c3 # line 2
< P> In your example, this will be 1, 0, 2
. Instead, instead of in line for
(also, please ), you can repeat through the list of line lists instead, and write each related line. Open with Infl ("Input. PDB", "R")> rows = [row in the infile for row] # Read all the input lines in the order of a list = Outfile as Open [1, 0, 2] Open ("Output.pdb", "w"): In order for idx: # Write output lines in the desired order. Outfile.write (line [idx])
Comments
Post a Comment