0

I have a code meant to strim a string within a large text file. When I run it an error message shows up "ValueError: need more than 1 value to unpack". So I changed the integer for 0, 1 and 2 in this line :

    _, k = line.split('\t',1)

If I input 2 it says 'Too many value to unpack'. I tried to define each variable as follow :

    f = []
    _ = []
    k = []
    x = []
    i = 0

.. That didn't work out either.

Here is the code:

    # Opens each file to read/modify
    infile='110331_HS1A_1_rtTA.result'
    outfile='2.txt'

    #import Regex
    import re

    with open (infile, mode='r', buffering=-1) as in_f, open (outfile, mode='w', buffering=-1) as out_f:
        f = (i for i in in_f if i.rstrip())
        for line in f:
            _, k = line.split('\t',1)
            x = re.findall(r'^1..100\t([+-])chr(\d+):(\d+)\.\.(\d+).+$',k)
            if not x:
                continue
            out_f.write(' '.join(x[0]) + '\n')

Here is the error :

   Traceback (most recent call last):
     File "C:\DB\Stack Overflow\test5.py", line 11, in <module>
       _, k = line.split('\t',1)
   ValueError: need more than 1 value to unpack

Thanks for those who are willing to help/teach me :)

madkitty
  • 1,657
  • 6
  • 24
  • 37

2 Answers2

2

You hit a line that doesn't contain \t, hence the resultant list only has one element.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Oh, that's the code I wrote on the other question. You could have asked there...

Since the file you gave didn't had any non-empty lines without tab, i assumed there would be no problem.

You can check if there's any tab character in the line by changing this one:

f = (i for i in in_f if i.rstrip())

to

f = (i for i in in_f if '\t' in i.rstrip())

So you'll remove empty lines and also those without the '\t'. To see the line of your file having that problem, you can do (using the original code):

try:
    _, k = line.split('\t',1)
except:
    print(line)

BTW, the _ is often used to things we don't care in some operation. It can have any name.

ATCGATTTCG....ATGC    1..100blablabla
       ^                     ^
 useless part (_)     important one (k)
Community
  • 1
  • 1
JBernardo
  • 32,262
  • 10
  • 90
  • 115
  • sorry i did but i though that you wouldn't see it anymay.. and this is really urgent T.T Anyway Thanks for your help, I'll try this in a couple of minutes and let you know. – madkitty Jul 14 '11 at 07:00
  • Good Morning, I changed f for `f = (i for i in in_f if '\t' in i.rstrip())` That worked out well, no error message and the output file contained all information separated by a space character. – madkitty Jul 15 '11 at 04:02
  • @madkitty if you want a tab character to separate, just change at the last line where it says `' '.join(...` to `'\t'.join(...` – JBernardo Jul 15 '11 at 16:28