-1

Value too many errors to unpack expected four. I am attempting to download a file from the internet that contains a list of peoples names and start dates in order to add them to a dictionary to be able to sort through them. I am having to adapt some existing code in order to streamline the code to speed up the process. I'm aware that line 33 is my primary issue and making both sides equal will solve correct the Value too many errors issue. I didn't know if there is a better way to write line 33 or just get rid of it in favor of a different method? Thanks

#!/usr/bin/env python3
import csv
import datetime
import requests

FILE_URL="http://marga.com.ar/employees-with-date.csv"

def get_start_date():
    """Interactively get the start date to query for."""

    print()
    print('Getting the first start date to query for.')
    print()
    print('The date must be greater than Jan 1st, 2018')
    year = int(input('Enter a value for the year: '))
    month = int(input('Enter a value for the month: '))
    day = int(input('Enter a value for the day: '))
    print()

    return datetime.datetime(year, month, day)

def get_file_lines(url):
    """Returns the lines contained in the file at the given URL"""

def get_file_lines(url):

    #Download the file over the internet
    response = requests.get(url, stream=True)
    lines = []

for line in response.inter_lines():
    x,y,z,a = line.decode("UTF-8")
    print("{},{},{},{}".format(x,y,z,a))
    lines.append(line.decode("UTF-8"))
return lines

def get_same_or_newer(start_date,data):
    """Returns the employees that started on the given date, or the closest one."""
    #Genera un csv
    reader = csv.reader(data[1:])
    #reader = data
    # We want all employees that started at the same date or the closest newer
    # date. To calculate that, we go through all the data and find the
    # employees that started on the smallest date that's equal or bigger than
    # the given start date.
    min_date = datetime.datetime.today()
    min_date_employees = []
    for row in reader:
        row_date = datetime.datetime.strptime(row[3], '%Y-%m-%d')

        # If this date is smaller than the one we're looking for,
        # we skip this row
        if row_date < start_date:
            continue
        # If this date is smaller than the current minimum,
        # we pick it as the new minimum, resetting the list of
        # employees at the minimal date.
        if row_date < min_date:
            min_date = row_date
            min_date_employees = []
        # If this date is smaller than the current minimum,
        # we pick it as the new minimum, resetting the list of
        if row_date == min_date:
            min_date_employees.append("{} {}".format(row[0], row[1]))
    return min_date, min_date_employees

def list_newer(start_date):
    data = get_file_lines(FILE_URL)
    while start_date < datetime.datetime.today():
        start_date, employees = get_same_or_newer(start_date,data)
        print("Started on {}: {}".format(start_date.strftime("%b %d, %Y"), employees))

        # Now move the date to the next one
        start_date = start_date + datetime.timedelta(days=1)

def main():
    start_date = get_start_date()
    list_newer(start_date)

if __name__ == "__main__":
    main()
cnixon
  • 1
  • 1
  • 2
    What do you expect `x,y,z,a = line.decode("UTF-8")` to do? Can you give an example of a line of content (in the text of the question, not in the comments) and how you would expect the program to deal with that? – Grismar Jun 02 '22 at 03:46
  • Welcome to Stack Overflow. ```line.decode("UTF-8")``` returns a string, not a list/tuple, so ```x,y,z,a = line.decode("UTF-8")``` will not work. Did you mean to split the ```line.decode("UTF-8")```? What is the format of ```line```? – ewong Jun 02 '22 at 03:47
  • 1
    Welcome to Stack Overflow. "and keep having issues with Value Error to many variables to unpack. Any help with understanding" Well, do you know what a `ValueError` is? Do you understand what variables are? Do you understand what unpacking is? "what is causing this issue" Well, do you see the part of the code that is highlighted in the error? It is showing the `x,y,z,a = line.decode("UTF-8")` part, right? How many things do you see on the left-hand side of the equals? Do you see that number in the error message? Do you see how that relates to "unpacking"? – Karl Knechtel Jun 02 '22 at 03:50
  • Next: what do you suppose will be "unpacked"? (Hint: what is on the other side of the equals sign?) How many things do you expect to be in there? Why? If there are more than four things, do you see why that would cause the problem being described in the error message? (If not, what do you think should happen instead?) – Karl Knechtel Jun 02 '22 at 03:51
  • If there are individual words or terms in an error message that you do not understand, the first thing you [should](https://meta.stackoverflow.com/questions/261592/) do is try to look them up, for example by using a search engine. Once you have a general picture of the error message, [try to trace through the logic of the code](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), and check the results of each calculation, in order to understand what is going on. If this does not solve the problem for you, it will at least make it possible to ask a proper, *specific* question. – Karl Knechtel Jun 02 '22 at 03:52

0 Answers0