Note that there are some explanatory texts on larger screens.

plurals
  1. PONumPy: load heterogenous columns of data from list of strings
    primarykey
    data
    text
    <p>I'm working with array data stored in an ASCII file (similar to <a href="http://thread.gmane.org/gmane.comp.python.numeric.general/42342" rel="nofollow">this thread</a>). My file is at least 2M lines (158 MB), and is divided into multiple sections with different schemas. In my module to read the format, I want to read the whole file via <code>lines = open('myfile.txt', 'r').readlines()</code>, so I can index the positions of each section, then read each section that I need into NumPy data structures.</p> <p>For example, one excerpt of a section is:</p> <pre><code>&gt;&gt;&gt; print lines[5:10] [' 1 0.1000 0.300E-03 0.000E+00 0.300E-03 0.000E+00 0.000E+00 0.300E-03 0.100E-03\n', ' 2 0.1000 0.120E-02 0.000E+00 0.120E-02 0.000E+00 0.000E+00 0.120E-02 0.100E-03\n', ' 3 0.1000 0.100E-02 0.000E+00 0.100E-02 0.000E+00 0.000E+00 0.100E-02 0.100E-03\n', ' 4 0.1000 0.110E-02 0.000E+00 0.110E-02 0.000E+00 0.000E+00 0.110E-02 0.100E-03\n', ' 5 0.1000 0.700E-03 0.000E+00 0.700E-03 0.000E+00 0.000E+00 0.700E-03 0.100E-03\n'] </code></pre> <p>Which has the schema <code>[int, float, float, float, float, float, float, float, float]</code>, and a later part will have have a simpler <code>[int, float]</code> schema:</p> <pre><code>&gt;&gt;&gt; print lines[20:25] [' 1 0.00000E+00\n', ' 2 0.43927E-07\n', ' 3 0.44006E-07\n', ' 4 0.44020E-07\n', ' 5 0.44039E-07\n'] </code></pre> <p>How can I quickly load in different sections of the lines with NumPy? I see there is <code>np.loadtxt</code>, but it requires a file handle, and reads all the way to the end. I also see <code>np.from*</code> functions, but I'm not sure how to use them with my already read <code>lines</code>. Do I need to read the file twice?</p> <p>With regards to the heterogeneous data types, I figure I can use a compound <code>dtype</code>, like <code>np.dtype([('col1', '&lt;i2'), ('col2', 'f4'), ('col3', 'f4'), ('col4', 'f4'), ('col5', 'f4'), ('col6', 'f4'), ('col7', 'f4'), ('col8', 'f4'), ('col9', 'f4')])</code>, correct?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload