How to compare 2 files and generate a list of matches, get value error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bridget Barker
    New Member
    • Jan 2011
    • 4

    How to compare 2 files and generate a list of matches, get value error?

    Code:
    f = open("Desktop/MEME_IDS", "r")  #open file with list of terms to search for
    patterns = {}  #create an open dictionary (this is a string)
    while True:
    	line = f.readline()  #read each line of file1, put into variable line
    	if (line == ''):    #at the end of file, stop
    		break
    	patterns[line] = None  #put each line into a list with the key patterns
    
    f = open("Desktop/genelistAFUA.txt", "r")  #open next file
    while True:
    	line = f.readline()  #read each line in, with break for end of file, as above
    	if (line == ''):
    		break
    	col1, col2 = line.split("\t")  #split the file into 2 columns, that are separated by a tab, what I want to match is in col1, info i need is in col2
    	if (col1 in patterns):   # if the value in col1 matches the patterns list above, then print col2
    		print col2

    #from A to parse sublist of genes i am interested in from full genelist
    #format of file one is a simple list of single values, the second file has 2 columns, I am matching column 1 from the list from file 1.

    #my error is :

    Traceback (most recent call last):
    File "Desktop/parsefile.py", line 14, in <module>
    col1, col2 = line.split("\t" ) #split the file into 2 columns, separated by a tab
    ValueError: need more than 1 value to unpack


    Any thoughts?
    Last edited by bvdet; Jan 13 '11, 12:41 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code.

    Instead of entering a while loop, iterate on the file object in a for loop. Example:
    Code:
    f = open("some_file")
    for line in f:
        print line
    Iteration will cease when the end of file is reached.

    Back to your problem. Check the contents of the last value of line.

    Comment

    • Bridget Barker
      New Member
      • Jan 2011
      • 4

      #3
      Thanks for your help. The program works if I take a few lines of the gigantor input file, beginning, middle or end, so i plan to just chop up the file, and go from there.

      I had originally had the program as you suggested, but it didn't work. And now realizing that it was probably the stupid input file from the get go.

      Also, wasn't sure how to to the code tags, my bad.
      Last edited by Bridget Barker; Jan 13 '11, 07:00 PM. Reason: mistype

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Glad to help. A lot of first time posters miss the code tags. The posters that never get it are the ones that are annoying!

        Comment

        • Bridget Barker
          New Member
          • Jan 2011
          • 4

          #5
          thanks, i mistook the [code] for needing to type [python] :)

          Also, another friend suggested using strip (i think this is similar to chomp, which i had used in perl) and that has totally solved it- I also had to keep the more complicated while=True loops, it just didn't work otherwise, ?????

          Comment

          Working...