Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98651

Re: new to python, help please !!

From Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: new to python, help please !!
Date 2015-11-11 11:06 -0600
Message-ID <mailman.246.1447261727.16136.python-list@python.org> (permalink)
References <93aef8e5-3d6f-41f4-a625-cd3c2007686e@googlegroups.com>

Show all headers | View raw


On 2015-11-11 08:34, Anas Belemlih wrote:
> i am  a beginning programmer,  i am trying to write a simple code
> to compare two character sets in 2 seperate files. ( 2 hash value
> files basically) idea is: open both files, measure the length of
> the  loop on.
> 
> if the length doesn't match, ==  files do not  match
> 
> if length matchs, loop  while comparing each character from each
> file if they match. please tell me what i am doing wrong ?  i am
> using python 2.7
> 
> **********************************
> hash1= open ("file1.md5", "r")
> line1 =hash1.read()
> hash2 = open("file2.md5","r")
> line2= hash2.read()
> 
> number1 = len(line1)
> number2 = len(line2)
> 
> #**************************
> i=0
> s1=line1[i]
> s2=line2[i]
> count = 0
> 
> if number1 != number2:
> 	print " hash table not the same size"
> else:
>     while count < number1:
> 	if s1 == s2:
> 		print " character", line1[i]," matchs"
> 		i=i+1
> 	count=count+1
> 	else
> 		print "Hash values corrupt"

Well, the immediate answer is that you don't update s1 or s2 inside
your loop.  Also, the indent on "count=count+1" is wrong.  Finally,
if the hashes don't match, you don't break out of your while loop.
That said, the pythonesque way of writing this would likely look
something much more like

  with open("file1.md5") as a, open("file2.md5") as b:
    for s1, s2 in zip(a, b):
      if s1 != s2:
        print("Files differ")

You can compare the strings to get the actual offset if you want, or
check the lengths if you really want a more verbatim translation of
your code:

  with open("file1.md5") as a, open("file2.md5") as b:
    for s1, s2 in zip(a, b):
      if len(s1) != len(s2):
        print("not the same size")
      else:
        for i, (c1, c2) in enumerate(zip(s1, s2)):
          if c1 == c2:
            print(" character %s matches" %  c1)
          else:
            print(" %r and %r differ at position %i" % (s1, s2, i))

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

new to python, help please !! Anas Belemlih <anas.belemlih@gmail.com> - 2015-11-11 08:34 -0800
  Re: new to python, help please !! John Gordon <gordon@panix.com> - 2015-11-11 16:58 +0000
  Re: new to python, help please !! Tim Chase <python.list@tim.thechases.com> - 2015-11-11 11:06 -0600
  Re: new to python, help please !! Ben Finney <ben+python@benfinney.id.au> - 2015-11-12 04:16 +1100
  Re: new to python, help please !! Quivis <quivis@domain.invalid> - 2015-11-11 17:48 +0000
    Re: new to python, help please !! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-11-12 13:58 +1100
      Re: new to python, help please !! Marko Rauhamaa <marko@pacujo.net> - 2015-11-12 08:21 +0200
        Re: new to python, help please !! Tim Chase <python.list@tim.thechases.com> - 2015-11-12 05:48 -0600
        Re: new to python, help please !! <paul.hermeneutic@gmail.com> - 2015-11-12 07:27 -0700
      Re: new to python, help please !! Quivis <quivis@domain.invalid> - 2015-11-12 17:55 +0000
        Re: new to python, help please !! Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-12 19:49 +0000
  Re: new to python, help please !! Peter Otten <__peter__@web.de> - 2015-11-12 15:56 +0100
  Re: new to python, help please !! Tim Chase <python.list@tim.thechases.com> - 2015-11-12 09:00 -0600
  Re: new to python, help please !! Peter Otten <__peter__@web.de> - 2015-11-12 16:41 +0100
  Re: new to python, help please !! Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-12 21:24 +0000

csiph-web