Wait... WHAT?

Michael Torrie torriem at gmail.com
Thu Feb 13 17:22:55 EST 2014


On 02/13/2014 10:46 AM, eneskristo at gmail.com wrote:
> 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.





More information about the Python-list mailing list