Connecting Tech Pros Worldwide Forums | Help | Site Map

Write with first argument as an int.

Newbie
 
Join Date: Jul 2009
Posts: 14
#1: Sep 11 '09
Somehow python will not let me open a file, and write an integer into this file.
My script goes something like this:
Expand|Select|Wrap|Line Numbers
  1. self.l__c = 4
  2. self.l__calculation = (self.status) #at this point, self.status is 16
  3.      if <condition>:
  4.           self.l__calculation = int(self.l__calculation) + (self.l__c)
  5.           l__write = open(self.my_file,'w').write(self.l__calculation)
  6.  
  7.  
  8. 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!

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#2: Sep 11 '09

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
#3: Sep 12 '09

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
#4: Sep 13 '09

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:

Expand|Select|Wrap|Line Numbers
  1. import pickle
  2.  
  3. def write(data, outfile):
  4.     f = open(outfile, "w+b")
  5.     pickle.dump(data, f)
  6.     f.close()
  7.  
  8. def read(filename, "r+b"):
  9.     f = open(filename)
  10.     data = pickle.load(f)
  11.     f.close()
  12.     return data
  13.  
  14. if __name__ == "__main__":
  15.     some_data = [123, 456, [0, 1, 2], "and a string!"]
  16.     write(some_data, "temp.file")
  17.     read_data = read("temp.file")
  18.     print read_data
  19.  
Hope this helps!
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#5: Sep 14 '09

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:
Expand|Select|Wrap|Line Numbers
  1. def fdim(s):
  2.     # return a float given a fraction (EX. '1/2')
  3.     ss = s.split('/')
  4.     return float(ss[0])/float(ss[1])
  5.  
  6. def evalList(s):
  7.     if s.strip().startswith('[' ) and s.strip().endswith(']'):
  8.         try: return eval(s)
  9.         except: pass
  10.     return s
  11.  
  12. def convertType(s):
  13.     for func in (int, float, fdim, evalList):
  14.         try:
  15.             n = func(s)
  16.             return n
  17.         except:
  18.             pass
  19.     return s
Newbie
 
Join Date: Jul 2009
Posts: 14
#6: Sep 21 '09

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
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#7: Sep 21 '09

re: Write with first argument as an int.


eksaII - By golly, I think you have it!

BV
Reply