0

Hi I have a list of False and True, which I have written to a file. When I read the file, I cant get back to my True and False list.... I only get the ["True","False"] version of it, but I want it to be like this: [True,False]
Is it possible to convert it? I have tried bool, it doesnt work..

This is my Read method.

readfromfile = open(tname,"r")
readfromtemp = open(tfname,"r")
read1 = readfromfile.read()
read2 = readfromtemp.read()
self._list = [int(n) for n in read1.split(',')]
self._cord = [n for n in read2.split(',')]
readfromfile.close()
readfromtemp.close()

Edited by ekkanh: n/a

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by ekkanh
0

There is no real difference:

if condition == True:
if condition == "True":

Edited by woooee: n/a

0

How about:

readfromfile = open(tname,"r")
readfromtemp = open(tfname,"r")
read1 = readfromfile.read()
read2 = readfromtemp.read()
self._list = [int(n) for n in read1.split(',')]
self._cord = [n != 'False' for n in read2.split(',')]
readfromfile.close()
readfromtemp.close()

woooee:

But not with 'False' used without ==

condition = 'False'
if condition:
    print 'Condition True'
else:
    print 'Condition False'

Edited by pyTony: n/a

0

I don't understand your point, unless it is to shorten the number of keystrokes for some reason.

condition = 'False'
if condition == 'True':
    print 'Condition True'
else:
    print 'Condition False'

Edited by woooee: n/a

0

usually it is prefered style not to use == and if the same statements must work before and after saving, you should use

if condition in (False, 'False'):
    do_stuff
else:
   true_stuff

That is prone to mistakes.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.