Title: Values: too many values to unpack (expected 18)
Tags: python;python-3.x;unpack
Question: I am trying to accomplish three things with the code but I keep getting stuck on the first step:
Cleared lists for every column in the csv file. Specifically for  indicator geocode geoarea timeperiod and value. Here I keep getting the too many values to unpack error. There are 18 columns. I tried including try and except but then my list was limited to 384 lines when there is way more. Do you have any ideas for workarounds?
Create lists for every line
Filter and select lists for the most recent year 2015
Here is the data that I have been working with: https://github.com/guillermocubells/sdg9b1_642
innovation_9b1 = open('Indicators_9_6.csv','r' , encoding = 'utf-8' , errors= 'ignore')
innovation_9b1.readline()
indicator= []
geocode = []
geoarea = []
timeperiod= []
value = []
for lines in innovation_9b1:
_,_,i,_,_,g,a,tp,v,_,_,_,_,_,_,_,_,_,= lines.strip().split(',')
indicator.append(i)
geocode.append(g)
geoarea.append(a)
timeperiod.append(tp)
print(lines)
Comment: Remove the last comma in `_,_,i,_,_,g,a,tp,v,_,_,_,_,_,_,_,_,_,`

Comment: Looks like a good case for ```pandas``` and ```csv.reader()```.

Comment: And the encoding of ```Indicators_9_6.csv``` seems to be ```iso-8859-1```, not ```utf-8```.

Here is the accepted answer: Given the variables you are using you would be better off like this:
for lines in innovation_9b1:
elements = lines.strip().split(',')
indicator.append(elements[2])
geocode.append(elements[5])
geoarea.append(elements[6])
timeperiod.append(elements[7])
print(lines)

Here is another answer: As I said in one of my comments, that is a good case for pandas:
import pandas as pd
from pathlib import Path
csv_file = Path('Indicators_9_6.csv')
delete_columns = [
'Goal',
'Target',
'SeriesCode',
'SeriesDescription',
'Value',
'Time_Detail',
'UpperBound',
'LowerBound',
'BasePeriod',
'Source',
'FootNote',
'Nature',
'Units',
'[Reporting Type]'
df = pd.DataFrame(
pd.read_csv(
csv_file,
encoding='iso-8859-1',
skipinitialspace=True
df.drop(delete_columns, axis=1, inplace=True)
Alternatively, you can just read the columns you want:
import pandas as pd
from pathlib import Path
csv_file = Path('Indicators_9_6.csv')
columns = [
'Indicator',
'GeoAreaCode',
'GeoAreaName',
'TimePeriod'
df = pd.DataFrame(
pd.read_csv(
csv_file,
encoding='iso-8859-1',
skipinitialspace=True,
usecols=columns
After that it is easy to refer to any column by its name:
&gt;&gt;&gt; df.Indicator
0       6.4.2
1       6.4.2
2       6.4.2
3       6.4.2
4       6.4.2
...
3529    9.b.1
3530    9.b.1
3531    9.b.1
3532    9.b.1
3533    9.b.1
Name: Indicator, Length: 3534, dtype: object
&gt;&gt;&gt;
And here is a proof of concept:
Python 3.7.5 (default, Oct 17 2019, 12:16:48)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; from pathlib import Path
&gt;&gt;&gt;
&gt;&gt;&gt; csv_file = Path('Indicators_9_6.csv')
&gt;&gt;&gt;
&gt;&gt;&gt; columns = [
...     'Indicator',
...     'GeoAreaCode',
...     'GeoAreaName',
...     'TimePeriod'
&gt;&gt;&gt;
&gt;&gt;&gt; df = pd.DataFrame(
...     pd.read_csv(
...         csv_file,
...         encoding='iso-8859-1',
...         skipinitialspace=True,
...         usecols=columns
...     )
&gt;&gt;&gt; df
Indicator  GeoAreaCode  GeoAreaName  TimePeriod
0        6.4.2            4  Afghanistan        2000
1        6.4.2            4  Afghanistan        2005
2        6.4.2            4  Afghanistan        2010
3        6.4.2            4  Afghanistan        2015
4        6.4.2            8      Albania        2000
...        ...          ...          ...         ...
3529     9.b.1          894       Zambia        2012
3530     9.b.1          894       Zambia        2013
3531     9.b.1          894       Zambia        2014
3532     9.b.1          894       Zambia        2015
3533     9.b.1          894       Zambia        2016
[3534 rows x 4 columns]
&gt;&gt;&gt;
I hope it helps.
Comment for this answer: Thanks! For the purpose of this project we are limited to what we create, so cannot really use pandas. But will keep in mind for further tasks

Here is another answer: You could also, instead of parsing the CSV manually, use Python's csv module, which allows you to parse CSV to lists or dicts.
For instance, since your data has headers, you could use csv.DictReader to parse it to a dict you can address by the headers. In this example csv.Sniffer is used to figure out the kind of CSV format you're using - I wasn't exactly sure what it was.
import csv
indicator = []
geocode = []
geoarea = []
timeperiod = []
value = []
with open('Indicators_9_6.csv', 'r', encoding='utf-8', errors='ignore') as innovation_9b1:
dialect = csv.Sniffer().sniff(innovation_9b1.read())
dialect.skipinitialspace = True
innovation_9b1.seek(0)
reader = csv.DictReader(innovation_9b1, dialect=dialect)
for row in reader:
indicator.append(row["Indicator"])
geocode.append(row["GeoAreaCode"])
geoarea.append(row["GeoAreaName"])
timeperiod.append(row["TimePeriod"])
print(row)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:11/28/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><python-3.x><unpack>', 'Title': 'Values: too many values to unpack (expected 18)', 'ViewCount': 110, 'id': 59087368, 'user_AboutMe': None, 'user_AccountId': 17199171.0, 'user_DisplayName': 'Guillermo Cubells', 'user_Reputation': 3.0, 'user_Views': 2.0, 'user_id': 12451287.0}}