0

How do I make the code so it reads the file and splits it so that it can assign which lines I specify into a certain named list. I want the first 3 lines to be assigned into a list then the next 3 and so on, so it would be:

list1 = Steve, Cat, v, 2, 3, 4, 1, 28

list2 = John, Dog, h , 6, 1, 7, 2, 45

list3 = (something)

text file below named character:

Steve
Cat
v 2 3 4 1 28
John
Dog
h 6 1 7 2 45

main code below

character_file = open("character.txt", 'r')
character_list = character_file.readlines()

for character in character_list:
    print(character)
    character_list_split = character.split()

    Steve = character_list_split[0,2]
    John = character_list_split[3,5]
    
character_file.close()

print(Steve)
print(John)

Thanks for the help I'm new to python

Infusion
  • 17
  • 5

1 Answers1

0

As mentioned in the comments, using a with block will close the file automatically once it goes out of scope.

This solution grabs your lines in blocks of 3. It then places the lists into a dictionary with the name as the key. It's not practical to put the lists into variables with the names as you have done.

with open("character.txt", 'r') as f:
    lines = f.readlines()
    names = {}
    for i in range(0, len(lines), 3):
        name = lines[i].strip()
        names[name] = [name, lines[i+1].strip()] + \
            lines[i+2].strip().split(' ')

As S3DEV has kindly mentioned in the comments, this is not very effective for large files and the file object itself can be iterated rather than reading in the entire file with readlines(). I've taken a shortcut and assumed that your file will always have 3 lines for each grouping and that your file will not end in the middle of an entry.

with open("character.txt", 'r') as f:
    names = {}
    while name := f.readline().strip():
        petname = f.readline().strip()
        information = f.readline().strip().split(' ')
        names[name] = [name, petname] + information

(note: walrus operator := requires Python 3.8 or above)

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • 2
    If reading a large file, the answer could be made more efficient by using a counter, rather than reading the entire file into memory. `f` can be iterated directly, thus removing the need to read the entire file into memory. – S3DEV May 28 '22 at 09:22
  • I wonder where `readlines` is still taught. In my opinion it should have been deprecated and thrown away when Python 3 was developed. – Matthias May 28 '22 at 11:52
  • I don't do much in the way of reading raw files anymore (csvs with Pandas and binary into NumPy, spoiled I know!). I'm assuming you mean using readline from f directly, but if I turn f into an iterator, there's no inherent way to just grab 3 off the top short of inslice that I found. – jonsca May 28 '22 at 21:19
  • @S3DEV Is that edit more in line with what you had in mind? (doing this more for my own edification at this point) – jonsca May 30 '22 at 08:54
  • 1
    @jonsca - Even more simple actually. Something along the lines of the *first* solution in [this answer](https://stackoverflow.com/a/5832971/6340496). – S3DEV May 30 '22 at 13:56
  • @S3DEV Ah, so my instinct about ´islice´ (made a typo above) was correct. My search-Fu obviously failed me on finding the linked question. – jonsca May 30 '22 at 21:50