By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

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

P: 4
Expand|Select|Wrap|Line Numbers
  2. f = open("Desktop/MEME_IDS", "r")  #open file with list of terms to search for
  3. patterns = {}  #create an open dictionary (this is a string)
  4. while True:
  5.     line = f.readline()  #read each line of file1, put into variable line
  6.     if (line == ''):    #at the end of file, stop
  7.         break
  8.     patterns[line] = None  #put each line into a list with the key patterns
  10. f = open("Desktop/genelistAFUA.txt", "r")  #open next file
  11. while True:
  12.     line = f.readline()  #read each line in, with break for end of file, as above
  13.     if (line == ''):
  14.         break
  15.     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
  16.     if (col1 in patterns):   # if the value in col1 matches the patterns list above, then print col2
  17.         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/", 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?
Jan 12 '11 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 2.5K+
P: 2,851
Please use code tags when posting code.

Instead of entering a while loop, iterate on the file object in a for loop. Example:
Expand|Select|Wrap|Line Numbers
  1. f = open("some_file")
  2. for line in f:
  3.     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.
Jan 13 '11 #2

P: 4
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.
Jan 13 '11 #3

Expert Mod 2.5K+
P: 2,851
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!
Jan 13 '11 #4

P: 4
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, ?????
Jan 13 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.