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!
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.
Comments