Write with first argument as an int.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ekasII
    New Member
    • Jul 2009
    • 16

    Write with first argument as an int.

    Somehow python will not let me open a file, and write an integer into this file.
    My script goes something like this:
    Code:
    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!
    Last edited by bvdet; Sep 11 '09, 12:53 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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.

    Comment

    • ekasII
      New Member
      • Jul 2009
      • 16

      #3
      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

      Comment

      • Hackworth
        New Member
        • Aug 2009
        • 13

        #4
        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:

        Code:
        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!

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          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:
          Code:
          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

          Comment

          • ekasII
            New Member
            • Jul 2009
            • 16

            #6
            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

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              eksaII - By golly, I think you have it!

              BV

              Comment

              Working...