0

thedeck1.txt

1 4 7 10 13 16 19 22 25
5 3 6 9 12 15 18 21 24
27 2 28 8 11 14 17 20 23 26 

thedeck2.txt

1 7
4
10 13 16
19 22 25 28 3
6
9
12 15 18 21 24 27 2 5 8 11 14 17 20 23 26

関数.py

    def readingdeck(file):
        file = open(deck1.txt(or also deck2.txt), 'r')

        total_deck = []
        string_counter = 0

        for line in file:
            newline = line.rstrip('\n')    
            for deck_char in newline:
                if (string_counter + 1 < len(newline)):
                    if (string_counter == 0) and (newline[string_counter + 1] == ' '):
                        total_deck.append(deck_char)
                    elif (string_counter == 0) and (newline[string_counter + 1] != ' '):
                        total_deck.append(int((str(newline[string_counter])) + str(newline[string_counter + 1])))                    
                    elif (string_counter >= 2):    
                        if (newline[string_counter] != ' ') and (newline[string_counter + 1] == ' ') and (newline[string_counter - 1] == ' '):
                            total_deck.append(deck_char)
                        elif (newline[string_counter] != ' ') and (newline[string_counter + 1] != ' '):
                            total_deck.append(int((str(newline[string_counter])) + str(newline[string_counter + 1])))
                    string_counter += 1
            string_counter = 0

        new_list = []    

        for item in total_deck:
            if type(item) == str:
                new_list.append(int(item))
            else:
                new_list.append(item)

        return new_list

やあ。だから私がこれをしたいのは印刷です

[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 3, 6, 9, 12, 15, 18, 21, 24, 4, 2, 5, 8, 11, 14, 17, 20, 23, 26]

(つまり、リスト内のすべての番号) thedeck1.txt に対しては正常に実行されますが、thedeck2.txt に対して試行すると失敗します (範囲外の文字列エラーが発生します)。

助言がありますか?

4

2 に答える 2