简体   繁体   中英

How do I unpack a namedtuple from a list of namedtuples in a file

In a file I have a list of tuples representing local stars:

('Sol', {'CNS': 'SUN', 'LHS': '--', 'short': 'Sol'}, 'G2.0 V', (255, 255, 0), '4.85', '1', '1+8P', Rect3(0.0, -0.0, 0.0), '8 planets')
('Proxima Centauri', {'CNS': 'GJ  551 C', 'LHS': '49', 'short': 'Proxima Centauri'}, 'M5.0 V', (192, 51, 0), '15.48', '0.11', '-', Rect3(-0.971736102445165, -0.7437324779154063, -0.44075536479508615), '"separation 7849"')
('alpha Centauri A', {'CNS': 'GJ  559 A', 'LHS': '50', 'short': 'alpha Centauri A'}, 'G2.0 V', (255, 255, 0), '4.38', '1.14', '3', Rect3(-1.0022795466990417, -0.8380977036473284, 0.2898459753310907), 'none')
('alpha Centauri B', {'CNS': 'GJ  559 B', 'LHS': '51', 'short': 'alpha Centauri B'}, 'K0 V', (255, 153, 0), '5.71', '0.92', '-', Rect3(-1.0021704312249438, -0.8378331027859452, 0.29098601110754313), '"orbit 18"')

I want to read these into memory and massage them into objects to use in the game. First I trim off the parentheses, but for some reason lin.strip(')(,') leaves the trailing parenthesis. Also, the unpacking nomen, idcodes,... = lin.split(',') fails saying too many to unpack (expected 9)

I've tried this code:

def setup_cosmos(starcount, _starfile='starcat_001.csv'):
    """ read in values from starfile, to populate the starmap.
    we'll use x,y horizontal plane,(+x right, +y back, +z north.) 
    """
    starlist = [ ]
    with open(_starfile, 'r') as starcat:
        num = 0
        for lin in starcat.readlines():
            # Unpack
            print("line #{0}: {1}".format(num, lin))
            lin = lin.strip(')(,'); print(lin)
            nomen, idcodes, spectral, colour, abs_mag, est_mass, num_obj, rect3, note = lin.split(',')
            # Make the new Star object
            orbitnum = random.randint(3) + 2
            mainworld = nomen +' ' + chr(orbitnum + 96)
            sdate = Datei(1, 2185)
            commods = []
            mon_prod = 0.0    # monthly production, an abstract economic number  ranging from 0.0 to 15.0+
            in_use = False
            star = Steorra(nomen, idcodes, spectral, colour, abs_mag, est_mass, num_obj, rect3, note, orbitnum, mainworld, sdate, commods, mon_prod, in_use)
            starlist.append(star)
            num += 1
            if num > starcount:
                break
            #. end for
        starcat.close()
    for ix in len(starlist):
        stars[ix] = create_new_star(starlist[ix])
        #. end for
    return None
    #. end def 
Traceback (most recent call last):
  File "D:\Google Drive\Python3-stuff\startraders\startrader.py", line 970, in <module>
    trader_setup(ST)
  File "D:\Google Drive\Python3-stuff\startraders\startrader.py", line 466, in trader_setup
    setup_cosmos(num_stars, _starfile)
  File "D:\Google Drive\Python3-stuff\startraders\startrader.py", line 394, in setup_cosmos
    nomen, idcodes, spectral, colour, abs_mag, est_mass, num_obj, rect3, note = lin.split(',')
ValueError: too many values to unpack (expected 9)

So I think the problem is the included tuples, idcodes, colour, and rect3. How do I get them included in the unpacking without upsetting the comma count?

It's incorrect to give that a "csv" extension. It's not a CSV file.

class Rect3(object):
    def __init__(self,_a,_b,_c):
        self.a = _a
        self.b = _b
        self.c = _c

for ln in open(_starfile):
    i1 = eval(ln)
    print( i1 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM