Calling local variable causes TypeError: cannot unpack non-iterable NoneType object

AR.Ward

Hi I was please wondering if I could have some help to understand why my self defined function works and gives the output I'm after, but when I try and assign variables to it outside of the function (to call header and rows separately) I get:

header,rows = parse_tsv(path) TypeError: cannot unpack non-iterable NoneType object

This is my code:

def parse_tsv(path):
    with open(path) as infile:

        header = []
        for line in infile:
            line = line.strip()
            header.append(line.split("\t")) 
            header = header[0]
            break

        rows = []
        for line in infile:
            line = line.strip()
            rows.append(line.split("\t"))
            if not line:
                continue
        rows = [[int(x) if x.isdigit() else x for x in i] for i in rows]
        rows = rows[1:]

    return print('header','=',header,'\n''rows','=',rows)

header,rows = parse_tsv(path)

header

I don't understand why it will give me a functioning and correct output with parser_tsv(path), but header,rows won't work. I don't think it can split them, but I am unsure how to go about fixing this. Thank you for your time!

andreashhp

That is because you are returning the output of print (which is always None). Just return header, rows instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am getting a "TypeError: cannot unpack non-iterable int object"

Error iterating over a list 'TypeError: cannot unpack non-iterable object'

TypeError: cannot unpack non-iterable NoneType object

TypeError: cannot unpack non-iterable int object in Django views function

TypeError: cannot unpack non-iterable int object

Python: TypeError: cannot unpack non-iterable int object

TypeError: cannot unpack non-iterable NoneType object when running a sorting function

tensorflow TypeError: cannot unpack non-iterable float object

This error occurs when I'm running my code on terminal "TypeError: cannot unpack non-iterable NoneType object"

(TypeError: cannot unpack non-iterable int object) for inversion counter using merge sort

Python TypeError: cannot unpack non-iterable bool object

I am getting: TypeError: cannot unpack non-iterable NoneType object, but I am returning a tuple

TypeError: cannot unpack non-iterable bool object

Python / TypeError: cannot unpack non-iterable Jugador object

Python 3.8.2| Why do I have a : TypeError: cannot unpack non-iterable NoneType object?

Error occurs running code in terminal "TypeError: cannot unpack non-iterable NoneType object"

TypeError at /cart/ cannot unpack non-iterable Cart object

-- TypeError: cannot unpack non-iterable NoneType object

Cannot unpack non-iterable object

PyAutoGui TypeError: cannot unpack non-iterable NoneType object

Find smaller number from 2 provided - works, but throws an error. TypeError: cannot unpack non-iterable NoneType object

TypeError: cannot unpack non-iterable GetColorImage object

TypeError: cannot unpack non-iterable Lexer object

TypeError: cannot unpack non-iterable int object (Python)

TypeError: cannot unpack non-iterable NoneType object Error

Python error- cannot unpack non-iterable NoneType object

"TypeError: cannot unpack non-iterable int object" with filter function

How to fix TypeError: cannot unpack non-iterable Button object

Python Django: TypeError: cannot unpack non-iterable MatchAll object