甲板1.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
甲板2.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 尝试它时它失败了(给出字符串超出范围错误)。
有什么建议么?