Re: [Tutor] output sequentially

2011-07-27 Thread Peter Otten
lina wrote:

 I have below file,
 
 I wish the output following:
 
 The first field 169 -170 sequential, and then the filed 2 from 1-29,
 ignore the rest 4 fields,
 
   
   169CHOL   O28 1612   6.966   6.060   6.429

Read the lines from the file, sort them with a proper key function, write 
the sorted lines to a new file:

with open(source.txt) as instream:
   lines = sorted(instream, key=mykey)

with open(dest.txt, w) as outstream:
outstream.writelines(lines)

Now for the mykey() function: what should it look like?
You want to sort by the integer value of the first two columns, so you have 
to split the lines into fields and then remove the non-digits from the 
fields you are interested in. Here's an outline:

def extract_int(field):
   only_digits = ...
   return int(only_digits)

assert extract_int(169CHOL) == 169
assert extract_int(H28) == 28

def mykey(line):
fields = ...
# example: ['169CHOL', 'H29', '1611', '6.963', '6.155', '6.395']
return extract_int(fields[0]), extract_int(fields[1])

assert mykey(169CHOL   H29 1611   6.963   6.155   6.395\n) == (169, 28)

Can you fill in the blanks?


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] output sequentially

2011-07-26 Thread lina
Hi,

I have below file,

I wish the output following:

The first field 169 -170 sequential, and then the filed 2 from 1-29,
ignore the rest 4 fields,

thanks for any suggestions,

  169CHOL   H29 1611   6.963   6.155   6.395
  169CHOL   O28 1612   6.966   6.060   6.429
  169CHOLC3 1613   7.005   6.073   6.566
  169CHOLC4 1614   6.940   5.950   6.629
  169CHOLC5 1615   7.006   5.934   6.766
  169CHOL   C10 1616   6.980   6.060   6.849
  169CHOLC1 1617   7.040   6.182   6.781
  169CHOLC2 1618   6.973   6.201   6.645
  169CHOLC6 1619   7.055   5.819   6.816
  169CHOLC7 1620   7.089   5.824   6.965
  169CHOLC8 1621   7.000   5.916   7.048
  169CHOLC9 1622   7.031   6.056   6.993
  169CHOL   C11 1623   7.000   6.169   7.092
  169CHOL   C12 1624   7.050   6.144   7.235
  169CHOL   C13 1625   6.993   6.009   7.281
  169CHOL   C14 1626   7.048   5.897   7.193
  169CHOL   C15 1627   6.982   5.781   7.266
  169CHOL   C16 1628   7.008   5.803   7.414
  169CHOL   C17 1629   7.038   5.952   7.414
  169CHOL   C19 1630   6.830   6.092   6.846
  169CHOL   C18 1631   6.841   6.000   7.265
  169CHOL   C20 1632   6.983   6.033   7.532
  169CHOL   C21 1633   7.094   6.057   7.635
  169CHOL   C22 1634   6.862   5.976   7.605
  169CHOL   C23 1635   6.838   6.050   7.737
  169CHOL   C24 1636   6.753   5.958   7.824
  169CHOL   C25 1637   6.609   6.008   7.837
  169CHOL   C26 1638   6.537   5.895   7.911
  169CHOL   C27 1639   6.620   6.116   7.945
  170CHOL   H29 1640   5.603   6.375   6.159
  170CHOL   O28 1641   5.625   6.472   6.165
  170CHOLC3 1642   5.604   6.501   6.303
  170CHOLC4 1643   5.560   6.645   6.330
  170CHOLC5 1644   5.539   6.683   6.477
  170CHOL   C10 1645   5.670   6.664   6.552
  170CHOLC1 1646   5.716   6.520   6.531
  170CHOLC2 1647   5.732   6.480   6.384
  170CHOLC6 1648   5.427   6.733   6.530
  170CHOLC7 1649   5.430   6.768   6.679
  170CHOLC8 1650   5.569   6.795   6.736
  170CHOLC9 1651   5.661   6.676   6.704
  170CHOL   C11 1652   5.792   6.713   6.775
  170CHOL   C12 1653   5.781   6.703   6.927
  170CHOL   C13 1654   5.695   6.824   6.964
  170CHOL   C14 1655   5.564   6.799   6.889
  170CHOL   C15 1656   5.462   6.893   6.952
  170CHOL   C16 1657   5.506   6.897   7.097
  170CHOL   C17 1658   5.637   6.821   7.105
  170CHOL   C19 1659   5.765   6.766   6.489
  170CHOL   C18 1660   5.769   6.948   6.913
  170CHOL   C20 1661   5.727   6.885   7.211
  170CHOL   C21 1662   5.832   6.780   7.247
  170CHOL   C22 1663   5.655   6.910   7.343
  170CHOL   C23 1664   5.749   6.980   7.441
  170CHOL   C24 1665   5.667   6.987   7.570
  170CHOL   C25 1666   5.745   7.071   7.672
  170CHOL   C26 1667   5.674   7.205   7.695
  170CHOL   C27 1668   5.760   6.998   7.805

-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor