# Problem involving namedtuple()

I am trying to port an old Star Trader game to Python3. I have a file of star data in this format:

``````# nomen,        Idcodes,      spectral,      colour,     abs_mag,      est_mass,     num_obj,        rect3,       notes
Star('Sol', Idcodes('SUN', '--',  'Sol'), 'G2.0 V', Colour(255, 255, 0), 4.85, 1, '1+8P', Rect3(0.0, -0.0, 0.0), '8 planets')
Star('Proxima Centauri', Idcodes('GJ  551 C','49',  'Proxima Centauri'), 'M5.0 V', Colour(192, 51, 0), 15.48, 0.123, '-', Rect3(-0.9717, -0.7437, -0.4407), '"separation 7849"')
Star('alpha Centauri A', Idcodes('GJ  559 A','50',  'alpha Centauri A'), 'G2.0 V', Colour(255, 255, 0), 4.38, 1.14, '3', Rect3(-1.0023, -0.83809, 0.28984), 'none')
Star('alpha Centauri B', Idcodes('GJ  559 B','51',  'alpha Centauri B'), 'K0 V', Colour(255, 153, 0), 5.71, 0.92, '-', Rect3(-1.0021, -0.83783, 0.29098), 'orbit 18" ')
``````

and my setup_cosmos() function is supposed to read this, unpack the data to add game-specific world stuff (like real exoplanet data!), and create stars, a sequence of star objects.
However, the read loop fails on the first iteration; apparently, there are too many values to unpack.

``````# ...
How many stars to begin with? 15
How long is the game in years? 5
How many tonnes of cargo can a ship carry? 25
# Now we are in setup_cosmos()
# These two lines are my debugging probes
line #0: Star('Sol', Idcodes('SUN', '--',  'Sol'), 'G2.0 V', Colour(255, 255, 0), 4.85, 1, '1+8P', Rect3(0.0, -0.0, 0.0), '8 planets')

star: Star('Sol', Idcodes('SUN', '--',  'Sol'), 'G2.0 V', Colour(255, 255, 0), 4.85, 1, '1+8P', Rect3(0.0, -0.0, 0.0), '8 planets')

# And here is the output on failure:
Traceback (most recent call last):
setup_cosmos(num_stars, _starfile)
nomen, idcodes, spectral, colour, abs_mag, est_mass, num_obj, rect3, note = star
ValueError: too many values to unpack (expected 9)
# But there seem to be 9 items in the unpacking tuple.
# So I played around in the debugger and found I could do fairly similar
# things with other namedtuples in the program:
d = Datei(270, 2185)
a, b = d
a
270
b
2185
# Even successfully run the line which failed:
star = Star('Sol', Idcodes('SUN', '--', 'Sol'), 'G2.0 V', Colour(255, 255, 0), 4.85, 1, '1+8P', Rect3(0.0, -0.0, 0.0), '8 planets')
nomen, idcodes, spectral, colour, abs_mag, est_mass, num_obj, rect3, note = star
nomen
'Sol'
idcodes
Idcodes(GJ='SUN', LHS='--', other='Sol')
note
'8 planets'
est_mass
1

``````

So I am at a loss to understand why Python3 won’t process my star-data file when I can do the same thing manually. What am I doing wrong?

Can you provide a full self-contained script that displays the error messages when executed? It’s a bit hard to dig through your mix of code and error messages to figure out what the executed stuff is.

Are you sure `star` is actually an instance of your namedtuple and not a string?

It would be difficult to say exactly what’s going on without seeing the actual code that failed. It seems that you have a file several hundred lines long that isn’t shown here, that does something that causes the problem. In particular;

Well, most likely it doesn’t do all of those things correctly. But we can’t see it here, so who knows.

But at a wild guess: when you read the data from a file, you are just… reading it, so you don’t actually get a `Star` object. You get a string which you read as a line from the file, where the first character is `S`, the next is `t` and so on. Of course that will have a lot more than the expected 9 elements.