使用类时,太多的值无法解包

时间:2020-04-02 20:01:00

标签: python python-3.x class

我一直收到错误消息:“ ValueError:太多值无法解包(预期为12)”。我检查了数据集,对所有变量进行了至少10次计数,然后把头撞在墙上....我在做什么错了?

代码(Python 3.7):

class Corona(object):
    def __init__(self, FIPS, County, State, Country, Updated, Latitude, Longitude,
                 Confirmed, Deaths, Recovered, Active, Combined_Key):
        self.FIPS = FIPS
        self.County = County
        self.State = State
        self.Country = Country
        self.Updated = Updated
        self.Latitude = Latitude
        self.Longitude = Longitude
        self.Confirmed = Confirmed
        self.Deaths = Deaths
        self.Recovered = Recovered
        self.Active = Active
        self.Combined_Key = Combined_Key


    def __str__(self):
        return self.FIPS + "/" + \
               self.County + "/" + \
               self.State + "/" + \
               self.Country + "/" + \
               self.Updated + "/" + \
               self.Latitude + "/" +\
               self.Longitude + "/" + \
               self.Confirmed + "/" + \
               self.Deaths + "/" + \
               self.Recovered + "/" + \
               self.Active + "/" + \
               self.Combined_Key

def loadPeople(filename, slist):
    ifile = open(filename, 'r')
    for line in ifile:
        FIPS, \
        County, \
        State, \
        Country, \
        Updated, \
        Latitude, \
        Longitude, \
        Confirmed, \
        Deaths, \
        Recovered, \
        Active, \
        Combined_Key, \
        = line.split(',')

        s = Corona(FIPS, County, State, Country, Updated, Latitude, Longitude,
                   Confirmed, Deaths, Recovered, Active, Combined_Key)
        slist.append(s)

def main():
    filename = "CoronaVirus.txt"
    sick = []
    loadPeople(filename, sick)

if __name__ == ("__main__"):
    main()

数据集的前三行

45001,Abbeville,South Carolina,US,2020-04-01 21:58:49,34.22333378,-82.46170658,4,0,0,0,"Abbeville, South Carolina, US"
22001,Acadia,Louisiana,US,2020-04-01 21:58:49,30.295064899999996,-92.41419698,47,1,0,0,"Acadia, Louisiana, US"
51001,Accomack,Virginia,US,2020-04-01 21:58:49,37.76707161,-75.63234615,7,0,0,0,"Accomack, Virginia, US"

3 个答案:

答案 0 :(得分:2)

在某些引号中包含逗号,因此这些字段在逗号中被分隔

s = '45001,Abbeville,South Carolina,US,2020-04-01 21:58:49,34.22333378,-82.46170658,4,0,0,0,"Abbeville, South Carolina, US"'
len(s.split(','))
# 14

使用csv module来代替手动拆分行:

import csv

with ifile as open(filename):
    reader = csv.reader(ifile)
    for line in reader:  # line is a list
        slist.append(Corona(*line))

答案 1 :(得分:-1)

简单的问题。但可能很难消除。当您用逗号分隔行时,还将最后一个具有完整位置的项目分成三部分。因此,您将获得两个额外的值("Abbeville, South Carolina, US")成为三个项目。尝试

for line in ifile:
    FIPS, \
    County, \
    State, \
    Country, \
    Updated, \
    Latitude, \
    Longitude, \
    Confirmed, \
    Deaths, \
    Recovered, \
    Active, \
    Combined_Key1, \
    Combined_Key2, \
    Combined_Key3 \
    = line.split(',')

然后:

    Combined_Key = ",".join(Combined_Key1, Combined_Key2, Combnied_Key3)

尽管由于组合键只是将County,State和Country组合在一起,所以您也可以尝试仅使用这三个组合作为组合键。但是请注意,如果您的任何行具有不同格式的组合键,那么您将需要一种更复杂的方法。

此外,如果您确定此文件是安全的,则可以使用csv来获取变量列表,尽管这需要花费更多的时间来学习。

答案 2 :(得分:-1)

实际上,从您的示例line.split(',')开始是不正确的,因为您将拆分分为14个而不是12个。以这种方式拆分时,您还需要在双引号字符内进行拆分。当您像这样分割以下行时,长度将为14,这不是您想要的。

45001,Abbeville,South Carolina,US,2020-04-01 21:58:49,34.22333378,-82.46170658,4,0,0,0,"Abbeville, South Carolina, US"

您可以使用以下方式修改loadPeople的定义

def loadPeople(filename, slist):
    ifile = open(filename, 'r')
    for line in ifile:
        FIPS, \
        County, \
        State, \
        Country, \
        Updated, \
        Latitude, \
        Longitude, \
        Confirmed, \
        Deaths, \
        Recovered, \
        Active, \
        Combined_Key, \
        = line.split('"')[0].split(',')[:-1]+[line.split('"')[1]]

        s = Corona(FIPS, County, State, Country, Updated, Latitude, Longitude,
                   Confirmed, Deaths, Recovered, Active, Combined_Key)
        slist.append(s)