Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError while trying to extract data from file

with open(st) as file:

    for line in file:

        line = line.rstrip()

        name, species, homeworld = line.split("\t")

        characters.append(StarWarsCharacter(name, species, homeworld))

I’m trying to extract data, but I just get a value error:

ValueError:
name, species, homeworld = line.split("\t")
ValueError: not enough values to unpack (expected 3, got 1)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First of all, the name of the file should be a string, the it should be "st". Second, the error is clear: it means that the split returns only one token, but you expected 3. Then, the file you are trying to read is not separated by "\t". In fact, trying to reproduce:

script.py:

with open("st") as file:
    for line in file:
        line = line.rstrip()
        ame, species, homeworld = line.split("\t")

Note that i didnt include the last line because I don’t have these structs.

st:

pluto   dog homeworld

Running the command python script.py i don’t have any error, because my file is correctly separated by "\t".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading