search between 2 files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • José

    search between 2 files

    I'm newby in Python, and I'm working on a script to manage files.
    I have two files, one with a list of domains, and the other with a new
    list of domains. I'd like to know if I have a repeated domain in the
    new's one. I tried opening with readline(), with readlines(), but truly
    I have no idea to countinue.

    Have anybody any question?

    thanks

  • Andres Rosado-Sepulveda

    #2
    Re: search between 2 files

    José wrote:[color=blue]
    > I'm newby in Python, and I'm working on a script to manage files.
    > I have two files, one with a list of domains, and the other with a new
    > list of domains. I'd like to know if I have a repeated domain in the
    > new's one. I tried opening with readline(), with readlines(), but truly
    > I have no idea to countinue.
    >
    > Have anybody any question?[/color]

    I think it would be something like this (iow, untested):

    domainsFile = file(filename, "rw")
    domains = f1.readlines()
    newDomainsFile = file(filename2, "r")
    for domain in newDomainsFile:
    ....if not domain in domains:
    ........domains .append(domain)

    --
    Andres Rosado
    Email: andresr@despamm ed.com
    ICQ: 66750646
    AIM: pantear
    Homepage: http://andres980.tripod.com/

    "Oh boo! Boo hoo! Oh! ... don't stop now ... you motorheads're killin'
    me."
    -- A surprisingly alive Rattrap, "A Better Mousetrap"

    Comment

    • Dang Griffith

      #3
      Re: search between 2 files

      On Wed, 04 Feb 2004 13:26:47 +0100, José <jmcalvar@telec able.es>
      wrote:
      [color=blue]
      >I'm newby in Python, and I'm working on a script to manage files.
      >I have two files, one with a list of domains, and the other with a new
      >list of domains. I'd like to know if I have a repeated domain in the
      >new's one. I tried opening with readline(), with readlines(), but truly
      >I have no idea to countinue.[/color]

      You said you wanted to know if you have a repeated domain in the new
      one. I am assuming you mean you want to know if there are any domains
      in the new list that are not in the old. If you just want to
      eliminate duplicates from the new list, independently of the first
      list, please clarify.

      import sets

      old_domains = sets.Set()
      for domain in file("olddomain s.txt"):
      old_domains.add (domain.strip() )

      new_domains = sets.Set()
      for domain in file("newdomain s.txt"):
      new_domains.add (domain.strip() )

      if new_domains == old_domains:
      print "There are no new domains."
      else:
      print "The following domains are new:"
      print new_domains - old_domains

      --dang
      p.s.
      Or if you like list comprehensions:

      import sets

      old_domains = sets.Set()
      [old_domains.add (domain.strip() ) for domain in file("olddomain s.txt")]

      new_domains = sets.Set()
      [new_domains.add (domain.strip() ) for domain in file("newdomain s.txt")]

      if new_domains == old_domains:
      print "There are no new domains."
      else:
      print "The following domains are new:"
      print new_domains - old_domains

      Comment

      • José

        #4
        Re: search between 2 files

        mmmh, it's not the same, but I try with this indications.

        I have a file called "data" from djbdns with different domain, for example:

        ----------------------
        ..domain.com:ns .domain.com
        +domain.com
        +.*domain.com
        ..domain1.com:n s.domain1.com
        +domain.com
        +.*domain.com
        ------------------------

        and have a new file for example "list" with new domains

        -------------------------
        newdomain.com
        newdomain1.com
        newdomain1.com
        -------------------------

        I'm programming a little interface in python, and before I modify the
        "data" file I'd like to know if I have in the list one domain I have in
        data.

        Thans




        José escribió:[color=blue]
        > I'm newby in Python, and I'm working on a script to manage files.
        > I have two files, one with a list of domains, and the other with a new
        > list of domains. I'd like to know if I have a repeated domain in the
        > new's one. I tried opening with readline(), with readlines(), but truly
        > I have no idea to countinue.
        >
        > Have anybody any question?
        >
        > thanks
        >[/color]

        Comment

        • José

          #5
          Re: search between 2 files

          I think with the both solutions it works if I have the files in the same
          format, so I think I'll call a script command to have the files in the
          same way, and when I learn more python I'll try with regular expressions

          :-))

          thank you very much


          José escribió:
          [color=blue]
          > mmmh, it's not the same, but I try with this indications.
          >
          > I have a file called "data" from djbdns with different domain, for example:
          >
          > ----------------------
          > .domain.com:ns. domain.com
          > +domain.com
          > +.*domain.com
          > .domain1.com:ns .domain1.com
          > +domain.com
          > +.*domain.com
          > ------------------------
          >
          > and have a new file for example "list" with new domains
          >
          > -------------------------
          > newdomain.com
          > newdomain1.com
          > newdomain1.com
          > -------------------------
          >
          > I'm programming a little interface in python, and before I modify the
          > "data" file I'd like to know if I have in the list one domain I have in
          > data.
          >
          > Thans
          >
          >
          >
          >
          > José escribió:
          >[color=green]
          >> I'm newby in Python, and I'm working on a script to manage files.
          >> I have two files, one with a list of domains, and the other with a new
          >> list of domains. I'd like to know if I have a repeated domain in the
          >> new's one. I tried opening with readline(), with readlines(), but
          >> truly I have no idea to countinue.
          >>
          >> Have anybody any question?
          >>
          >> thanks
          >>[/color]
          >[/color]

          Comment

          Working...