0

I've got a .dat file that i I've pulled the data from and i was using the tabulate plug in to tidy it up and put it into tables. However, part of the question is to add a position column or counter. It should be simple enough to add one element at the start of each list in my array but i am having an absolute nightmare...

the code for pulling in the data from the .dat file is:

def readToDictionary():
    global dicts
    fi = open('CarRegistry.dat', 'r')

    dicts = []
    buffer = []

    while True:
              
        team = fi.readline()
        if not team: break

        fields = team.split(',')
        buffer.append(fields)

    fi.close()

    dicts = buffer
    print(dicts)
    
    return dicts

Im duplicating the array deliberately as i need to do some other functions on it and want to keep the original data intact.

The raw out put is: [['1', 'BD61 SLU', 'HONDA', 'CR-V', 'SFDR', '5', '1780', '4510', '130', '39', 'True\n'], ['2', 'CA51 MBE', 'CHEVROLET', 'CORVETTE', 'JTAV', '2', '1877', '1234', '194', '24', 'True\n'], ['3', 'PC14 RSN', 'FORD', 'F-150', 'PQBD', '5', '2121', '5890', '155', '20', 'True\n'], ['4', 'MB19 ORE', 'HONDA', 'ACCORD', 'FDAR', '5', '1849', '4933', '125', '47.3', 'False\n'], ['5', 'BD68 NAP', 'HONDA', 'ACCORD', 'FDAV', '5', '1849', '4933', '171', '37.7', 'False\n']...

what i want to get to is:

[['1', '1', 'BD61 SLU', 'HONDA', 'CR-V', 'SFDR', '5', '1780', '4510', '130', '39', 'True\n'], ['2', '2', 'CA51 MBE', 'CHEVROLET', 'CORVETTE', 'JTAV', '2', '1877', '1234', '194', '24', 'True\n'], ['3', '3', 'PC14 RSN', 'FORD', 'F-150', 'PQBD', '5', '2121', '5890', '155', '20', 'True\n'], ['4', '4', 'MB19 ORE', 'HONDA', 'ACCORD', 'FDAR', '5', '1849', '4933', '125', '47.3', 'False\n'], ['5', '5', 'BD68 NAP', 'HONDA', 'ACCORD', 'FDAV', '5', '1849', '4933', '171', '37.7', 'False\n']...

It's basically a counter at the start of each list.

Ive tried all sorts and just keep getting errors, probably because i cant understand the basics of why i cant just do this:


    for i in buffer:
        buffer.insert(i, i+1)

to go through each entry in the list and add a value equal to the index +1... I know its probably simple but i've been banging my head off the monitor for a good few hours now...

1
  • 1
    If you just want to duplicate that first field, do for row in buffer: / row.insert( 0, row[0]). Commented Dec 22, 2022 at 20:01

1 Answer 1

1

The key is, you don't want to manipulate buffer. You want to manipulate the individual lists within buffer:

for i,row in enumerate(buffer):
    row.insert( 0, str(i+1) )
Sign up to request clarification or add additional context in comments.

3 Comments

OMG you absolute hero! Thank you! Been at this assignment for an age and literally input that line and its worked perfectly! Seriously, thank you so much! I knew what i wanted to do but for the life of me couldn't find and example to follow.
so am i right in thinking the enumerate command goes through each of the lists in sequence rather than trying to go through the top level like i was doing? And the row.insert( 0 = the index and str(i+1) is converting the value to a string?
No and yes. for i,row in enumerate(buffer) is the same as for row in buffer:, except that enumerate also returns a counter as it goes through the list. When you had for i in buffer, i is getting each list in turn, not the index of the item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.