开发者

program for comparisons in python

开发者 https://www.devze.com 2023-03-21 18:46 出处:网络
I\'m reall开发者_如何学Cy new to programming (really, really new) and need help with the basics. I\'m trying to write a program with python that will compare the contents of two .txt files, one a refe

I'm reall开发者_如何学Cy new to programming (really, really new) and need help with the basics. I'm trying to write a program with python that will compare the contents of two .txt files, one a reference and the other the source. The contents are a simple random listing of names, and I want it to print out if there are any names in the source that are not in the reference.

I've looked at other stuff on this site but every time I tried it, the terminal would never actually give a result, even if there was a print command in the program.

I also have a hard time reading the language of a program and ascertaining it's exact function, so something with clear directions would be really appreciated.

As far as I have is:

ref = open("reference.txt")
sor = open("source.txt")

list1 = ref.read()
list2 = sor.read()

for i in list2:
    if i != list:
    print i

ref.close()
sor.close()

And when I try and run this, it says "expected an indented block"? at the 'print i' line. Why?

Please help me out, as I have to teach myself this stuff and am not doing too well.

Thanks.


If you are totally, completely new to programming then it will take you some time to be able to implement what you describe. Take a step back, pour yourself a beverage, and start here. Start at the beginning, and repeat each illustration until you understand.

http://docs.python.org/tutorial/


As previously mentioned, your inner if statement needs to be indented, as

for i in list2:
    if i != list:
        print i

This requires two indents because it is two nested blocks. As a basic rule of thumb, anywhere you're ending a line with a colon (:), you're starting a new code block, and should be indenting another level. This is so you can un-indent once to end the if block without ending the for block.

However, I doubt this will do what you want based on your description. It's likely you wanted something more like

sourceLines = set(sor.readLines())
for line in ref.readlines():
    if line not in sourcelines:
        print line


if blocks in python have to be indented, add another level of indent for your print i statement

for i in list2:
    if i != list:
        print i


These lines read the files as strings:

list1 = ref.read()
list2 = sor.read()

This loop iterates through the string one character at a time:

for i in list2:

This line compares the character to the list class:

    if i != list:


I'll answer your indentation error first: you need another 4 spaces before the print statement. In Python indentation is important and you need to indent any block and dedent to end that block.

For your problem I am going to not give you written out code in advance but more of a flow on how to do it:

  1. Create 2 sets (http://docs.python.org/library/stdtypes.html#set-types-set-frozenset)
  2. Read both files into a seperate set (you can do this while iterating over a file and appending to your set).
  3. Compare your two sets using the set1 - set2 syntax (see the link above) to show all items not common to both sets.

Hope you can make it work from this.

Now for the code:

with open('file1.txt') as file1:
    set1 = set(line for line in file1)

with open('file2.txt') as file2:
    set2 = set(line for line in file2)

print set1 - set2

This uses some principles you are probably not familiar with (look up: list comprehensions, generator comprehensions and the previously noted link about sets which are unique collections).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号