gpt4 book ai didi

Python 错误 : need more than one value to unpack

转载 作者:行者123 更新时间:2023-11-28 22:56:29 24 4
gpt4 key购买 nike

好的,我正在学习 Python。但是为了我的学习,我已经不得不做相当复杂的事情了。我正在尝试运行一个脚本来分析 excel 文件中的数据。这是它的样子:

#!/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)])

这些是 excel 文件中数据列的标题,我想拆分它们,我什至不确定在使用 excel 文件中的数据时是否有必要。

#more irrelevant stuff

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

这是它应该在我的输出中写入的内容,“共享”是我计算出来的,其余的已经在文件中了。

好吧,现在我的问题是,当我这样调用脚本时:
python script.py CNVB_reads.403476 CNVB_reads.403447 script.csv 在我的 shell 中

我收到以下错误消息:

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

我不知道与数据相关的是什么意思……有什么想法吗?

最佳答案

line.split('\t', 10) 调用未返回十一个元素。也许它是空的?

您可能想使用 csv module而不是解析这些文件。

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])

关于Python 错误 : need more than one value to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15569453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com