Robin Thorben
Robin Thorben

Reputation: 89

Python error: need more than one value to unpack

Ok, so I'm learning Python. But for my studies I have to do rather complicated stuff already. I'm trying to run a script to analyse data in excel files. This is how it looks:

#!/usr/bin/python
import sys

#lots of functions, not relevant

resultsdir = /home/blah

filename1=sys.argv[1]
filename2=sys.argv[2]
out = open(sys.argv[3],"w")

#filename1,filename2="CNVB_reads.403476","CNVB_reads.403447"

file1=open(resultsdir+"/"+filename1+".csv")
file2=open(resultsdir+"/"+filename2+".csv")

for line in file1:
    start.p,end.p,type,nexons,start,end,cnvlength,chromosome,id,BF,rest=line.split("\t",10)
    CNVs1[chr].append([int(start),int(end),float(BF)])

for line in file2:
    start.p,end.p,type,nexons,start,end,cnvlength,chromosome,id,BF,rest=line.split("\t",10)
    CNVs2[chr].append([int(start),int(end),float(BF)])

These are the titles of the columns of the data in the excel files and I want to split them, I'm not even sure if that is necessary when using data from excel files.

#more irrelevant stuff

out.write(filename1+","+filename2+","+str(chromosome)+","+str(type)+","+str(shared)+"\n")

This is what it should write in my output, 'shared' is what I have calculated, the rest is already in the files.

Ok, now my question, finally, when I call the script like that:
python script.py CNVB_reads.403476 CNVB_reads.403447 script.csv in my shell

I get the following error message:

start.p,end.p,type,nexons,start,end,cnvlength,chromosome,id,BF,rest=line.split("\t",10)
ValueError: need more than 1 value to unpack

I have no idea what is meant by that in relation to the data... Any ideas?

Upvotes: 2

Views: 3177

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121486

The line.split('\t', 10) call did not return eleven elements. Perhaps it is empty?

You probably want to use the csv module instead to parse these files.

import csv
import os

for filename, target in ((filename1, CNVs1), (filename2, CNVs2)):
    with open(os.path.join(resultsdir, filename + ".csv"), 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter='\t')
        for row in reader:
            start.p, end.p = row[:2]
            BF = float(row[8])
            target[chr].append([int(start), int(end), BF])

Upvotes: 4

Related Questions