beginner in python

Tim Williams tim at tdw.net
Thu Aug 2 11:41:55 EDT 2007


On 02/08/07, Beema shafreen <beema.shafreen at gmail.com> wrote:
> Hi everybody ,
>       I am a beginner in python,
>          I have to fetch the redundant entries from a file,
>
> code:
>
> import re
> L = []
> fh = open('ARCHITECTURE_MAIN.txt',
> 'r')
> for line in fh.readlines():
>         data =line.strip()
> #       splitted = data.split('#')
>         L.append(data)
> fh.close()
>
>
>
> M=L
> for x in L:
>         x = x.split('#')
>         for y in M:
>             y = y.split('#')
>         x_data = x[0],x[1],x[2],x[3]
>         #print x_data
>         y_data = y[0],y[1],y[2],y[3]
>         #print y_dat
>         if x_data[0] == y_data[0]:
>               print x_data
>
>
> i get the result as a tupule,
> the text file which has datas separated by hash
> entry#isoform#start#stop#  i have to check upto this
>
> 00250_1#ARCH_104#61#89#Literature#9224948#00250####
> 00250_1#ARCH_104#97#126#Literature#9224948#00250####
> 00250_1#ARCH_104#139#186#Literature#9224948#00250####
> 00251_1#ARCH_463#7#59#SMART##00251####
> 00251_1#ARCH_463#91#121#SMART##00251####
> 00251_1#ARCH_463#251#414#SMART##00251####
> 00251_1#ARCH_463#540#624#SMART##00251####
> 00252_1#ARCH_474#1#21#Literature#8136357#00252####
> 00252_1#ARCH_393#481#501#Literature#8136357#00252####
> 00252_1#ARCH_463#523#553#SMART##00252####
> 00253_1#ARCH_82#37#362#SMART##00253####
> 00253_1#ARCH_54#365#522#SMART##00253####
> 00253_1#ARCH_104#589#617#SMART##00253####
> 00253_1#ARCH_104#619#647#SMART##00253####
> 00253_1#ARCH_104#684#712#SMART##00253####
> 00254_1#ARCH_82#27#352#SMART##00254####
> 00254_1#ARCH_54#355#510#SMART##00254####
> 00254_1#ARCH_104#576#604#SMART##00254####
> 00254_1#ARCH_104#606#634#SMART##00254####
> 00254_1#ARCH_104#671#699#SMART##00254####
> 00255_1#ARCH_82#56#425#SMART##00255####
> 00255_1#ARCH_54#428#582#SMART##00255####
> 00255_1#ARCH_104#696#724#SMART##00255####
>
>
>
>
> can you suggest me ,what are the improvement i have to make in the above
> code
> regards
> shafreen

Shafreen,   your code snippet (as you posted it) prints any lines that
end with an entry code equal the entry code on the last line of the
file, with these lines split at #.

The whole thing (as you posted it) could probably be written something
like this:

===================
# not tested or optimised

fh = open('ARCHITECTURE_MAIN.txt').read().splitlines()
last_code = fh[-1].split('#')[0]
for line in fh:
        if out_line.startswith(last_code):
            print out_line.split('#')
# it will always print at least the last line of the file
===================

HTH :)



More information about the Python-list mailing list