title: Best way to save objects to text files in python?
body: <p>I am writing a simple artificial neural net in python. I need to be able to save all of the data for a given net into a text file. Specifically, I need to save a list of nodes with the following properties:
  name, layer number, delta value, connection weights</p>
  
  <p>Right now, I've created my own data format. It looks like this:</p>
  
  <pre><code>nodeName:layer,delta,connectionWeights;
  </code></pre>
  
  <p>e.g.</p>
  
  <pre><code>a:1,.5,b-.5#c-.7;b:1,.75,c-.3#d-.4;c:2,.3,a-12#b-.4
  </code></pre>
  
  <p>It's actually fairly easy to create this data from an object and parse it to create an object from the data. However, the code is ugly and very specific to the format I've created. For context, here is the code I've written to parse:</p>
  
  <pre><code>   def readInput(self, datafile):
          f = open(datafile, 'r')
          DNAstring = f.read()
          f.close()
          #first, parse each node
          allNodes = DNAstring.split(";")
          for nodey in allNodes:
              n = nodey.partition(":") #partition separates node name from its data
              name = n[0]
              data = n[2].split(",") #each data element is separated by a comma
              layer = data[0]
              delta = data[1]
              connections = {}
              connectionList = data[2].split("#") #each connection is delimited by '#'
              for c in connectionList:
                  x = c.partition("-")
                  name = x[0]
                  weight = x[2]
                  connections.update({name:weight})
                  print("updating connection with ", name, weight)
              #now we have all the data for a node, create a node object
              n = Node(name, layer, delta, connections)
              self.nodes.update({name:n})
  </code></pre>
  
  <p>My question is this: is there an easier way to go about this process? It seems to me like there must be an easier solution than creating a new data format and parsing function each time one wants to save an object to a text file.
  Should I try to use an existing format like XML or JSON to make my code more understandable/modifiable? How would I go about doing this in python?</p>
  
  <p>Thanks</p>