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 🙂
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:
to
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):
BTW, the
_is often used to things we don’t care in some operation. It can have any name.