简体   繁体   中英

How to append lines from text file to multiple lists in python 3x

My problem is that I have a text file which contains this:

♦J
♣J
♠J
♣8
♦A
♥9
♥J
♥J
♦A
♦K
♦7
♦J
♦7
♦A
♦K
♥7
♣10
♣J
♠A
♦A
♣J
♦7
♠10
♥K
♣9
♥10
♦A
♠8
♠J
♥9
♦8
♠A

And I am trying to append this in to 8 different lists in order from to bottom. I have 8 different lists and a list that contains all the different lists they are set like this:

deckOne = []
deckTwo = []
DeckThree = []
DeckFour = []
deckFive = []
deckSix = []
deckSeven = []
deckEight = []
deckList = [deckOne, deckTwo, DeckThree, DeckFour, deckFive, deckSix, deckSeven, deckEight]

I get no errors when I run the program, but, I do not get the output that I am expecting. the expected output would look like this:

[♦J, ♣J, ♠J, ♣8]
[♦A, ♥9, ♥J, ♥J]
[♦A, ♦K, ♦7, ♦J]
[♦7, ♦A, ♦K, ♥7]
[♣10, ♣J, ♠A, ♦A]
[♣J, ♦7, ♠10, ♥K]
[♣9, ♥10, ♦A, ♠8]
[♠J, ♥9, ♦8, ♠A]

But the result I am getting is currently this:

[['♦J', '♦A', '♣10', '♣9'], ['♣J', '♦K', '♣J', '♥10'], ['♠J', '♦7', '♠A', '♦A'], ['♣8', '♦J', '♦A', '♠8'], ['♦A', '♦7', '♣J', '♠J'], ['♥9', '♦A', '♦7', '♥9'], ['♥J', '♦K', '♠10', '♦8'], ['♥J', '♥7', '♥K', '♠A']]

This output is wrong is because it appends every 4 lines in the file instead of 4 lines to one list, then the next 4 to the next list, and so on. the code I am trying to use to do this, is this:

def hentLagretSpill():
    import codecs
    with codecs.open("game_save.txt", 'rb', encoding="utf-8") as gameGetter:
        count = 0
        for i, l in enumerate(gameGetter):
            count += 1
            deckList[i % 8].append(l.strip())
        print(deckList)

But I have also tried this, and it works, but it is very lengthy:

        x = -1
        for line in gameGetter:
            x += 1
            if x // 4 == 0:
                deckOne.append(line.strip('\n'))
            if x // 4 == 1:
                deckTwo.append(line.strip('\n'))
            if x // 4 == 2:
                DeckThree.append(line.strip('\n'))
            if x // 4 == 3:
                DeckFour.append(line.strip('\n'))
            if x // 4 == 4:
                deckFive.append(line.strip('\n'))
            if x // 4 == 5:
                deckSix.append(line.strip('\n'))
            if x // 4 == 6:
                deckSeven.append(line.strip('\n'))
            if x // 4 == 7:
                deckEight.append(line.strip('\n'))

How would I solve this?

The problem you are seeing is that the % (modulo) operator returns the remainder, not the integer division as in your second example. You can fix it by going back to the integer division, and using that as the index in the list of lists.

def hentLagretSpill():
    with open("game_save.txt", 'r', encoding="utf-8") as gameGetter:
        for i, l in enumerate(gameGetter):
            ix = i // 4
            deckList[ix].append(l.strip())
        print(deckList)

# prints:
[['♦J', '♣J', '♠J', '♣8'],
 ['♦A', '♥9', '♥J', '♥J'],
 ['♦A', '♦K', '♦7', '♦J'],
 ['♦7', '♦A', '♦K', '♥7'],
 ['♣10', '♣J', '♠A', '♦A'],
 ['♣J', '♦7', '♠10', '♥K'],
 ['♣9', '♥10', '♦A', '♠8'],
 ['♠J', '♥9', '♦8', '♠A']]

Another way, Using itertools.islice

Assuming you have the file contents in f

from itertools import islice
to_split=iter(f.strip().splitlines())
split_list=[list(islice(to_split,4)) for _ in range(8)]
for i in split_list:
    print(i)

On second thoughts you can do so without islice , using list slicing

split_list=[to_split[split_idx:split_idx+4] for split_idx in range(0,len(to_split),4)]

This prints

['♦J', '♣J', '♠J', '♣8']
['♦A', '♥9', '♥J', '♥J']
['♦A', '♦K', '♦7', '♦J']
['♦7', '♦A', '♦K', '♥7']
['♣10', '♣J', '♠A', '♦A']
['♣J', '♦7', '♠10', '♥K']
['♣9', '♥10', '♦A', '♠8']
['♠J', '♥9', '♦8', '♠A']

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