Write with first argument as an int. | Newbie | | Join Date: Jul 2009
Posts: 14
| |
Somehow python will not let me open a file, and write an integer into this file.
My script goes something like this: -
self.l__c = 4
-
self.l__calculation = (self.status) #at this point, self.status is 16
-
if <condition>:
-
self.l__calculation = int(self.l__calculation) + (self.l__c)
-
l__write = open(self.my_file,'w').write(self.l__calculation)
-
-
-
self.l__calculation = int(self.l__calculation) + (self.l__c)
<---when printed, this line works just fine, it returns 20 (16 + 4), I then want to write 20 to a my_file, but python tells me that my first argument must be string and not int, I WANT TO WRITE ONLY AN INT TO THIS FILE, NOT STRING!
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,566
| | | re: Write with first argument as an int.
Please use code tags when posting code!
You must convert the integer to a string to write it to a file. Please see Python documentation here.
| | Newbie | | Join Date: Jul 2009
Posts: 14
| | | re: Write with first argument as an int.
The point of writting an int is to do calculations, I can't calculate strings.
eg on string: 16 + 4 = 164
eg on int: 16 + 4 = 20
| | Newbie | | Join Date: Aug 2009 Location: Louisville
Posts: 13
| | | re: Write with first argument as an int.
Write it to the file as a string, when you read it back, cast it as an int()?
On the other hand if you really do want to save a data type to a file, I suggest you check the Python manual for the pickle module. Example: -
import pickle
-
-
def write(data, outfile):
-
f = open(outfile, "w+b")
-
pickle.dump(data, f)
-
f.close()
-
-
def read(filename, "r+b"):
-
f = open(filename)
-
data = pickle.load(f)
-
f.close()
-
return data
-
-
if __name__ == "__main__":
-
some_data = [123, 456, [0, 1, 2], "and a string!"]
-
write(some_data, "temp.file")
-
read_data = read("temp.file")
-
print read_data
-
Hope this helps!
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,566
| | | re: Write with first argument as an int.
Excellent post, Hackworth. Many of my applications write data to a text file, and I convert the data from string as required when the data is read. I used pickle in the past, but decided to get away from it so my data files will be readable and because the amount of data is rather small. Each value is passed to a function which will attempt to type cast it to integer, float, formatted dimension (as in 1/2 to 0.5 units), or list. If the type castings fail, the original string is returned. Therefore tuples will be returned as strings which suit my purposes. The point is, the data is written as a string and read as a string, so choose the method that best suits you to type cast your integers.
Here's my function: - def fdim(s):
-
# return a float given a fraction (EX. '1/2')
-
ss = s.split('/')
-
return float(ss[0])/float(ss[1])
-
-
def evalList(s):
-
if s.strip().startswith('[' ) and s.strip().endswith(']'):
-
try: return eval(s)
-
except: pass
-
return s
-
-
def convertType(s):
-
for func in (int, float, fdim, evalList):
-
try:
-
n = func(s)
-
return n
-
except:
-
pass
-
return s
| | Newbie | | Join Date: Jul 2009
Posts: 14
| | | re: Write with first argument as an int.
Thats too complicated, heres how:
save the int as string (as requested) on text file
read text and save value on memory
do the calculations (value on memory + int)
save this result as value on memory
write value on memory to text as string.
And Vwala! the calculation is done.
Thanks for the reply's
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,566
| | | re: Write with first argument as an int.
eksaII - By golly, I think you have it!
BV
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|