CSV阅读器没有足够的值来解包(预期11,得1)

时间:2018-05-29 16:11:10

标签: python csv

使用csv阅读器时遇到问题。 我想逐行读取csv文件并打印出每行的第二个条目。 一行的所有被写入一个以','

分隔的单元格末尾

看起来像这样: 0.00,457.00,0.00,862.00,12892,32575, “LAX”,11292,30325, “DEN”,11,

我试过这段代码:

    with open(file_path, 'r', newline='') as file:
        reader = csv.reader(file, delimiter=',')
        for line in reader:
            passangers, freight, mail, distance, origin_airport_id, origin_city_marked_id, origin, dest_airport_id, dest_city_marked_id, dest, month = line
            print(freight)

但是我得到:ValueError:没有足够的值来解包(预期11,得到1)

这段代码不久前在另一个项目中为我工作,所以我真的不知道它为什么现在不起作用。我尝试了不同的分隔符(';',空格等),但基本上是猜测,因为我不知道还能做什么

2 个答案:

答案 0 :(得分:0)

问题是由于您尝试将包含12个元素的列表压缩为11个元素。

尝试:

<?xml version="1.0" encoding="UTF-8"?>
<basepage:BasePage
    xmlns                         ="http://xamarin.com/schemas/2014/forms"
    xmlns:x                        ="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class                        ="Biocorp.AppClasses.PagesAndViewModels.Order.AddOrderPage"
    xmlns:basepage                 ="clr-namespace:Biocorp.AppClasses.PagesAndViewModels.Base;assembly=Biocorp"
    xmlns:i18n                     ="clr-namespace:Biocorp;assembly=Biocorp"
    xmlns:rendered                 ="clr-namespace:Biocorp.AppClasses.Rendered;assembly=Biocorp"
    NavigationPage.HasNavigationBar="true"
    Title                          ="{i18n:Translate AddOrder_Order}">
  <ContentPage.Content>
    <RelativeLayout
        HorizontalOptions="Fill">
      <ScrollView
          RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=0}"
          RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=0}"
          RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=0}"
          RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-53}">
        <StackLayout
            Orientation      ="Vertical"
            Spacing          ="0"
            Padding          ="0,7,0,7"
            VerticalOptions  ="Start"
            HorizontalOptions="Fill">
          <RelativeLayout
              HeightRequest    ="64"
              HorizontalOptions="Fill">
            <rendered:BaseButton
                IsEnabled                      ="false"
                Text                           =""
                BackgroundColor                ="{StaticResource GrayTextBackground}"
                BorderRadius                   ="5"
                RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=14}"
                RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=7}"
                RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-28}"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-14}" />
            <StackLayout
                Orientation                    ="Horizontal"
                Spacing                        ="5"
                RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=14}"
                RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=7}"
                RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-28}"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-14}">
              <StackLayout
                  Orientation      ="Vertical"
                  Spacing          ="0"
                  Padding          ="15,5,15,5"
                  VerticalOptions  ="Fill"
                  HorizontalOptions="FillAndExpand">
                <rendered:BaseLabel
                    Text                   ="{i18n:Translate AddOrder_Date}"
                    Style                  ="{StaticResource LabelStyle}">
                </rendered:BaseLabel>
                <rendered:BaseLabel
                    Text             ="{Binding Date}"
                    Style            ="{StaticResource TextboxStyle}" >
                </rendered:BaseLabel>
                  <StackLayout.GestureRecognizers>
                  <TapGestureRecognizer
                      Command="{Binding DateCommand}" />
                </StackLayout.GestureRecognizers>
              </StackLayout>
              <StackLayout
                  Orientation      ="Vertical"
                  Spacing          ="0"
                  Padding          ="0,0,0,0"
                  VerticalOptions  ="Fill"
                  HorizontalOptions="End"
                  WidthRequest     ="40">
                <Image
                    Source           ="ic_date"
                    Aspect           ="AspectFit"
                    WidthRequest     ="15"
                    HeightRequest    ="15"
                    VerticalOptions  ="CenterAndExpand"
                    HorizontalOptions="CenterAndExpand" />
              </StackLayout>
            </StackLayout>
          </RelativeLayout>
          <RelativeLayout
              HeightRequest    ="64"
              HorizontalOptions="Fill">
            <rendered:BaseButton
                IsEnabled                      ="false"
                Text                           =""
                BackgroundColor                ="{StaticResource GrayTextBackground}"
                BorderRadius                   ="5"
                RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=14}"
                RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=7}"
                RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-28}"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-14}" />
            <StackLayout
                Orientation                    ="Horizontal"
                Spacing                        ="5"
                RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=14}"
                RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=7}"
                RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-28}"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-14}">
              <StackLayout
                  Orientation      ="Vertical"
                  Spacing          ="0"
                  Padding          ="15,5,15,5"
                  VerticalOptions  ="Fill"
                  HorizontalOptions="FillAndExpand">
                <rendered:BaseLabel
                    Text                   ="{i18n:Translate AddOrder_Client}"
                    Style                  ="{StaticResource LabelStyle}">
                </rendered:BaseLabel>
                <rendered:BaseLabel
                    Text             ="{Binding Client}"
                    Style            ="{StaticResource TextboxStyle}">
                </rendered:BaseLabel>
                <StackLayout.GestureRecognizers>
                  <TapGestureRecognizer
                      Command="{Binding SearchClientCommand}" />
                </StackLayout.GestureRecognizers>
              </StackLayout>
              <StackLayout
                  Orientation      ="Vertical"
                  Spacing          ="0"
                  Padding          ="0,0,0,0"
                  VerticalOptions  ="Fill"
                  HorizontalOptions="End"
                  WidthRequest     ="40">
                <Image
                    Source           ="ic_client_name"
                    Aspect           ="AspectFit"
                    WidthRequest     ="15"
                    HeightRequest    ="15"
                    VerticalOptions  ="CenterAndExpand"
                    HorizontalOptions="CenterAndExpand" />
              </StackLayout>
            </StackLayout>
          </RelativeLayout>
          <RelativeLayout
              HeightRequest    ="64"
              HorizontalOptions="Fill">
            <rendered:BaseButton
                IsEnabled                      ="false"
                Text                           =""
                BackgroundColor                ="{StaticResource GrayTextBackground}"
                BorderRadius                   ="5"
                RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=14}"
                RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=7}"
                RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-28}"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-14}" />
            <StackLayout
                Orientation                    ="Horizontal"
                Spacing                        ="5"
                RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=14}"
                RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=7}"
                RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-28}"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-14}">
              <StackLayout
                  Orientation      ="Vertical"
                  Spacing          ="0"
                  Padding          ="15,5,15,5"
                  VerticalOptions  ="Fill"
                  HorizontalOptions="FillAndExpand">
                <rendered:BaseLabel
                    Text                   ="{i18n:Translate AddOrder_Remarks}"
                    Style                  ="{StaticResource LabelStyle}" >
                </rendered:BaseLabel>
                <rendered:BorderlessEntry
                    Text             ="{Binding Remarks}"
                    Style            ="{StaticResource TextboxStyle}" >
                </rendered:BorderlessEntry>
              </StackLayout>
              <StackLayout
                  Orientation      ="Vertical"
                  Spacing          ="0"
                  Padding          ="0,0,0,0"
                  VerticalOptions  ="Fill"
                  HorizontalOptions="End"
                  WidthRequest     ="40">
                <Image
                    Source           ="ic_your_remarks"
                    Aspect           ="AspectFit"
                    WidthRequest     ="15"
                    HeightRequest    ="15"
                    VerticalOptions  ="CenterAndExpand"
                    HorizontalOptions="CenterAndExpand" />
              </StackLayout>
            </StackLayout>
            <RelativeLayout.GestureRecognizers>
              <TapGestureRecognizer
                  Tapped         ="OnTapGestureRecognizerForFocus" />
            </RelativeLayout.GestureRecognizers>
          </RelativeLayout>
        </StackLayout>
      </ScrollView>
      <RelativeLayout
          RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=0}"
          RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-53}"
          RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=0}"
          RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=53}">
        <rendered:BaseButton
            x:Name                         ="cancelButton"
            Command                        ="{Binding CancelCommand}"
            CommandParameter               ="{x:Reference cancelButton}"
            Text                           ="{i18n:Translate App_Cancel}"
            FontSize                       ="14"
            TextColor                      ="White"
            BackgroundColor                ="{StaticResource BlueDark_073B74}"
            FontAttributes                 ="Bold"
            BorderRadius                   ="5"
            RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.022, Constant=0}"
            RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=6}"
            RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.467, Constant=0}"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-13}" />
        <rendered:BaseButton
            x:Name                         ="saveButton"
            Command                        ="{Binding ProceedCommand}"
            CommandParameter               ="{x:Reference saveButton}"
            Text                           ="{i18n:Translate App_Proceed}"
            FontSize                       ="14"
            TextColor                      ="{StaticResource BlueDark_073B74}"
            BackgroundColor                ="{StaticResource Golden_F5AF01}"
            FontAttributes                 ="Bold"
            BorderRadius                   ="5"
            RelativeLayout.XConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.511, Constant=0}"
            RelativeLayout.YConstraint     ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=6}"
            RelativeLayout.WidthConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.467, Constant=0}"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-13}" />
      </RelativeLayout>
    </RelativeLayout>
  </ContentPage.Content>
</basepage:BasePage>

原始问题的简化示例:

    passangers, freight, mail, distance, origin_airport_id, origin_city_marked_id, origin, dest_airport_id, dest_city_marked_id, dest, month, element = line

答案 1 :(得分:0)

我强烈建议您使用csv.DictReader来构建逻辑。在这里命名如此大量的变量可能会导致混淆。

这是我们为fieldnames参数提供列表的示例。请注意, 12个字段,因为您的行以逗号结尾。由于field_list只有11个元素,因此最后一列会分配键None。打印字典时很明显。

from io import StringIO
import csv

mystr = StringIO('0.00,457.00,0.00,862.00,12892,32575,"LAX",11292,30325,"DEN",11,')

field_list = ['passengers', 'freight', 'mail', 'distance', 'origin_airport_id',
              'origin_city_marked_id', 'origin', 'dest_airport_id',
              'dest_city_marked_id', 'dest', 'month']

# replace mystr with open(file_path, 'r', newline='')
with mystr as file:
    reader = csv.DictReader(file, delimiter=',', fieldnames=field_list)
    for line in reader:
        print(line)

OrderedDict([('passengers', '0.00'),
             ('freight', '457.00'),
             ('mail', '0.00'),
             ('distance', '862.00'),
             ('origin_airport_id', '12892'),
             ('origin_city_marked_id', '32575'),
             ('origin', 'LAX'),
             ('dest_airport_id', '11292'),
             ('dest_city_marked_id', '30325'),
             ('dest', 'DEN'),
             ('month', '11'),
             (None, [''])])