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

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
3
  • That's very low-level. Is there some reason you're working character by character, like an assignment requirement or something? Commented Oct 20, 2013 at 0:54
  • No, I just want it to add the numbers to a list. No assignment. Commented 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. Commented Oct 20, 2013 at 0:57

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

You sir are fabulous. Thank you very much.
0

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

2 Comments

Just a note: strip isn't necessary, since split with no sep argument ignores leading or trailing whitespace.
@Brionius Well, you learn something new every day... Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.