Wait... WHAT?

E

eneskristo

Can we please revert back to the original problem?
def save():
target = open ("save.swroc", 'w')
target.write([counter, loop, number_of_competitors, competitors])
def load():
the_array = list(open("save.swroc", 'r'))
the_array = target
counter = the_array[0]
loop = the_array[1]
number_of_competitors = the_array[2]
competitors = the_array[3]
Is this better?
 
M

MRAB

Can we please revert back to the original problem?
def save():
target = open ("save.swroc", 'w')

This opens the file for writing text (assuming you're using Python 3).
target.write([counter, loop, number_of_competitors,
competitors])

This tries to write a list to the file. You can't do that. A list isn't
text.
def load():
the_array = list(open("save.swroc", 'r'))

This open the file for reading text. Using 'list' will make it read
lines of text and return them as a list.
the_array = target

What's 'target'?
counter = the_array[0]

This will set 'counter' to the first line of text that was read.
loop = the_array[1]

This will set 'loop' to the second line of text.
number_of_competitors = the_array[2]

This will set 'number_of_competitors' to the third line of text.
competitors = the_array[3]

This will set 'number_of_competitors' to the fourth line of text.
Is this better?
Not really! :)

Have a look at the "pickle" module, or the "json" module.
 
M

Michael Torrie

Can we please revert back to the original problem?
def save():
target = open ("save.swroc", 'w')
target.write([counter, loop, number_of_competitors, competitors])
^^^^^^^^^
Have you tried to run this code? Does it even produce a file? On my
python it says that write() is expecting a string on Python 3, or a
character buffer object on Python 2.7. Have you broken out the code
into a minimal, standalone file you can work on?
def load():
the_array = list(open("save.swroc", 'r'))
^^^^^^^^^^^
That's better. You know it reads in the text file one line at a time
into a list right? This would work if your file was actually written
with one variable in text form on each line.
the_array = target
^^^^^^^^^^
You have now reassigned the_array to an undefined object. If target is
defined somewhere (I can't see that it is here in your code snippet),
then you've now lost the array of lines you just read in.
counter = the_array[0]
loop = the_array[1]
number_of_competitors = the_array[2]
competitors = the_array[3]
Is this better?

Well it doesn't run, so we can't say it's better.

A couple of points/questions/hints/suggestions:

1. make a minimal, complete, example of what you are trying to do. Code
you can run without the rest of your program.

2. What are your variables, "counter," "loop," "number_of_competitors,"
"competitors?"

3. What format is the file supposed to be in?

4. If you pullup the file in an editor does it look right? (IE do you
know what the output from your save function actually looks like?)

5. If you're dealing with numbers, remember the text file has no concept
of numbers. You'll have to parse them from text when you read them.

6. Consider using the pickle module if you really want to store and load
python objects without encoding and decoding a text file.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top