title: How to write a dictionary to a text file with columns for each element in the dictionary in Python
body: <p>I am trying to use a previously generated Workspace from matlab to create a input text file for another program to read from. I can import the Workspace into Python with no issues as a dictionary. From here I am having difficulty writing to a text file due to the different data types and different sized arrays in the dictionary. I would like for each field in the dictionary to have its own column in the text file but have had little luck.
  Here is a screen shot of the imported dictionary from matlab.
  <a href="https://i.stack.imgur.com/RPI88.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RPI88.png" alt="enter image description here" /></a></p>
  <p>I would like the text file to have this format. (the header is not necessary)
  <a href="https://i.stack.imgur.com/UQMuR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UQMuR.png" alt="enter image description here" /></a></p>
  <p>I was able to get one of the variables into the text file with the following code but, I can't add more variables or ones with different data types.</p>
  <pre class="lang-py prettyprint-override"><code>import csv
  import scipy.io
  import numpy as np
  #import json
  #import _pickle as pickle
  #import pandas as pd
  
  mat = scipy.io.loadmat('Day055.mat')
  #print(type(mat))
  #print(mat['CC1'])
  CC1 = mat['CC1']
  CC2 = mat['CC2']
  DP1 = mat['DP1']
  #print(CC1)
  #print(CC2)
  
  dat = np.array([CC1, DP1])
  dat =dat.T
  
  #np.savetxt('tester.txt', dat, delimiter = '\t')
  np.savetxt('tester.txt', CC1, delimiter = '\t')
  
  '''
  with open('test.csv', 'w') as f:
      writer = csv.writer(f)
      for row in CC1:
          writer.writerow(row)
  #print(type(CC1))
  
  #print(&quot;CC1=&quot;, CC1)
  #print(&quot;first entry to CC1:&quot;, CC1[0])
  mat = {'mat':mat}
  
  df = pd.DataFrame.from_dict(mat)
  print(df)
  
  print(type(mat))
  
  x=0
  
  with open('inputDay055.txt', 'w') as file:
      for line in CC1:
          file.write(CC1[x])
          #file.write(&quot;\t&quot;.join(map(str, CC2))+&quot;\n&quot;)
      #file.write(pickle.dumps(mat))
          x=x+1
  '''
  
  print(&quot;all done&quot;)
  </code></pre>
  <p>As you can see I have tried a few different ways as well but commented them out when I was not succesful.</p>