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
function.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
Hi. So what i want this to do is print
[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]
(ie, all of the number in a list) it does it successfully for thedeck1.txt, but it fails (gives a string out of range error) when i try it for thedeck2.txt.
Any suggestions?
asked Oct 20, 2013 at 0:50
Michael C
1411 gold badge2 silver badges7 bronze badges
-
That's very low-level. Is there some reason you're working character by character, like an assignment requirement or something?DSM– DSM2013年10月20日 00:54:12 +00:00Commented Oct 20, 2013 at 0:54
-
No, I just want it to add the numbers to a list. No assignment.Michael C– Michael C2013年10月20日 00:55:45 +00:00Commented Oct 20, 2013 at 0:55
-
That's good news-- in that case, you can save yourself a lot of effort, as in @Brionius' answer.DSM– DSM2013年10月20日 00:57:28 +00:00Commented Oct 20, 2013 at 0:57
2 Answers 2
That seems like a pretty complicated method. Here's a simpler way:
def read_deck(filename):
with open(filename, 'r') as f: # Opens the file and assigns the file object to the name `f` (also makes sure the file will be closed properly)
contents = f.read().split() # Reads the entire file, then splits it into a list of strings by whitespace (i.e. a space, a newline, whatever)
return [int(x) for x in contents] # Converts the list of strings to a list of ints
answered Oct 20, 2013 at 0:55
Brionius
14.2k3 gold badges41 silver badges50 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Michael C
You sir are fabulous. Thank you very much.
Here is an annotated function
import itertools
def read_file(file_name):
# Open the file
with open(file_name) as fn:
# This line makes an iterable that contains each number in your
# file, but stored as a string.
# `.split()` separates the numbers by whitespace
file_contents = [line.split() for line in fn]
# Turn all the strings into numbers
# itertools.chain is a way to make this list of lists into a single list
return [int(num) for num in itertools.chain(*file_contents)]
answered Oct 20, 2013 at 0:58
SethMMorton
49.5k13 gold badges72 silver badges90 bronze badges
2 Comments
Brionius
Just a note:
strip isn't necessary, since split with no sep argument ignores leading or trailing whitespace.SethMMorton
@Brionius Well, you learn something new every day... Thanks!
lang-py