How do I get astropy Column to store string of any length?
<p>I'm generating a few catalogues, and would like to have a column for comments. For some reason, when I generate the column and try to store a comment it only takes the first character. </p>

<pre><code>from astropy.table import Column

C1 = Column(['']*12, name = 'ID')
C1[4] = 'test comment' 
</code></pre>

<p>Then </p>

<pre><code>print C1[4]
&gt;&gt; t 
</code></pre>

<p>Looking at C1, I see that <code>&lt;Column name='ID' dtype='str1' length=12&gt;</code>
so it's obviously only storing a 1 char string. </p>

<p>if I try </p>

<pre><code>C2 = Column(['some really long silly string']*12, name = 'ID')
C2[4] = 'test comment' 
</code></pre>

<p>then</p>

<pre><code>print C1[4]
&gt;&gt; test comment
</code></pre>

<p>but again, I can only store up to a 29 char string because <code>&lt;Column name='ID' dtype='str29' length=12&gt;</code> and this is a terrible solution anyway. </p>

<p>How do I tell Column to store any length string?</p>

<p>For this use case I usually first collect the data as a Python list of strings and then call the <code>astropy.table.Column</code> constructor.</p>

<pre><code>&gt;&gt;&gt; from astropy.table import Column
&gt;&gt;&gt; data = ['short', 'something longer']
&gt;&gt;&gt; Column(data=data, name='spam')
&lt;Column name='spam' dtype='str3' length=2&gt;
  a
bbb
</code></pre>

<p>The <code>Column</code> will convert your data to a Numpy array with fixed width <code>dtype</code> for strings of the appropriate length (and left pad shorter strings with spaces).</p>

<p>Similarly, when constructing <code>astropy.table.Table</code> objects, I usually first collect the data as a Python list of dicts of row data, and then let the <code>Table</code> constructor figure out the appropriate <code>dtype</code> automatically.</p>

<pre><code>&gt;&gt;&gt; from astropy.table import Table
&gt;&gt;&gt; rows = [{'ham': 42, 'spam': 'a'}, {'ham': 99, 'spam': 'bbb'}]
&gt;&gt;&gt; table = Table(rows=rows, names=['spam', 'ham'])
&gt;&gt;&gt; table
&lt;Table length=2&gt;
spam  ham 
str3 int64
---- -----
   a    42
 bbb    99
</code></pre>

<p>Of course this isn't super fast or memory-efficient, but for my applications it's good enough.</p>

<p>More generally, note that working with strings stored in Numpy arrays (which is what <code>astropy.table.Column</code> is doing) simply is painful (in my opinion, no offense intended to Numpy developers or people that like it). The best support I'm aware of for this comes from <code>pandas</code>, so you could use <code>pandas</code> to work with your data and use the <code>to_pandas</code> and <code>from_pandas</code> method of <code>astropy.table.Table</code> if you need an Astropy table, e.g. to read / write to FITS files or do something else that <code>pandas.DataFrame</code> doesn't support.</p>

python string ascii astropy