Note that there are some explanatory texts on larger screens.

plurals
  1. POpandas.read_csv from string or package data
    text
    copied!<p>I have some csv text data in a package which I want to read using read_csv. I was doing this by </p> <pre><code>from pkgutil import get_data from StringIO import StringIO data = read_csv(StringIO(get_data('package.subpackage', 'path/to/data.csv'))) </code></pre> <p>However, StringIO.StringIO disappears in Python 3, and io.StringIO only accepts Unicode. Is there a simple way to do this?</p> <p><strong>Edit</strong>: the following does not appear to work </p> <pre><code>import pandas as pd import pkgutil from io import StringIO def get_data_file(pkg, path): f = StringIO() contents = unicode(pkgutil.get_data('pymc.examples', 'data/wells.dat')) f.write(contents) return f wells = get_data_file('pymc.examples', 'data/wells.dat') data = pd.read_csv(wells, delimiter=' ', index_col='id', dtype={'switch': np.int8}) </code></pre> <p>failing with </p> <pre><code> File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 401, in parser_f return _read(filepath_or_buffer, kwds) File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 209, in _read parser = TextFileReader(filepath_or_buffer, **kwds) File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 509, in __init__ self._make_engine(self.engine) File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 611, in _make_engine self._engine = CParserWrapper(self.f, **self.options) File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 893, in __init__ self._reader = _parser.TextReader(src, **kwds) File "parser.pyx", line 441, in pandas._parser.TextReader.__cinit__ (pandas/src/parser.c:3940) File "parser.pyx", line 551, in pandas._parser.TextReader._get_header (pandas/src/parser.c:5096) pandas._parser.CParserError: Passed header=0 but only 0 lines in file </code></pre>
 

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