Can i become more precise like instead of printing all lines for PHE and ASP is it possible that for PHE python will print only those lines which will have information about H and HA and for ASP it will print those lines which will have information about HA and HB.
Thanks > On Tue, 14 Jul 2009 18:48:06 +0100, David Gibb <deg...@gmail.com> wrote: > > [Something top-posted, which I've shuffled down] > >> On Tue, Jul 14, 2009 at 1:33 PM, <amr...@iisermohali.ac.in> wrote: >>> Dear all, >>> >>> Can anyone tell me that suppose i have a file having content like: >>> >>> Â Â _Atom_name >>> Â Â Â _Atom_type >>> Â Â Â _Chem_shift_value >>> Â Â Â _Chem_shift_value_error >>> Â Â Â _Chem_shift_ambiguity_code >>> Â Â 1 Â Â 1 Â PHE Â Â Â H Â Â H Â Â Â 8.49 Â Â 0.02 Â Â >>> 1 >>> Â Â 2 Â Â 1 Â PHE Â Â Â HA Â Â H Â Â Â 4.60 Â Â 0.02 Â Â >>> 1 >>> Â Â 3 Â Â 1 Â PHE Â Â Â CA Â Â C Â Â 57.83 Â Â Â 0.3 Â Â >>> 1 >>> Â Â 4 Â Â 2 Â LEU Â Â Â H Â Â H Â Â Â 8.23 Â Â 0.02 Â Â >>> 1 >>> Â Â 5 Â Â 2 Â LEU Â Â Â HA Â Â H Â Â Â 4.25 Â Â 0.02 Â Â >>> 1 >>> Â Â 6 Â Â 2 Â LEU Â Â Â HB2 Â H Â Â Â 1.54 Â Â 0.02 Â Â >>> 1 >>> Â Â 7 Â Â 3 Â ASP Â Â Â H Â Â H Â Â Â 8.10 Â Â 0.02 Â Â >>> 1 >>> Â Â 8 Â Â 3 Â ASP Â Â Â HA Â Â H Â Â Â 4.52 Â Â 0.02 Â Â >>> 1 >>> Â Â 9 Â Â 3 Â ASP Â Â Â HB2 Â H Â Â Â 2.65 Â Â 0.02 Â Â >>> 1 >>> stop >>> >>> >>> now what i want that instead of copying all the lines it will just >>> write >>> the information about PHE and ASP then how i acn do that using python >>> programming.Kindly tell me the command for that. > >> try something like: >> >> for line in open("filename").readlines(): >> if (re.search("PHE|ASP",line): >> print line >> > > The readlines() is unnecessary, and if your file is at all long you'd > be better off precompiling the regular expression. > > import re > > expr = re.compile("PHE|ASP") > with open("filename") as f: > for line in f: > if expr.search(line): > print line > > -- > Rhodri James *-* Wildebeest Herder to the Masses > -- > http://mail.python.org/mailman/listinfo/python-list > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA -- http://mail.python.org/mailman/listinfo/python-list