从文件对话框中选择(csv)文件后,如何读取此类文件?界面显示该文件不存在。
使用代码:
input_form = """
<div style="border:solid navy; padding:20px;">
<input type="file" id="file_selector" name="files[]"/>
<output id="list"></output>
</div>
"""
javascript = """
<script type="text/Javascript">
function handleFileSelect(evt) {
var kernel = IPython.notebook.kernel;
var files = evt.target.files; // FileList object
console.log('Executing orig')
console.log(files)
// files is a FileList of File objects. List some properties.
var output = [];
var f = files[0]
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
'</_Mli>');
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
var command = 'fname = "' + f.name + '"'
console.log(command)
kernel.execute(command);
}
document.getElementById('file_selector').addEventListener('change', handleFileSelect, false);
</script>
"""
def file_selector():
from IPython.display import HTML, display
display(HTML(input_form + javascript))
f=file_selector()
我得到这个输出,详细的错误是
> IOErrorTraceback (most recent call last)
> <ipython-input-31-3234c73d353d> in <module>()
> 2 get_ipython().system(u'ls ~/library')
> 3 print fname
> ----> 4 df = pandas.read_csv(fname, index_col=False, header=0);
>
> /home/nbuser/anaconda2_501/lib/python2.7/site-packages/pandas/io/parsers.pyc
> in parser_f(filepath_or_buffer, sep, delimiter, header, names,
> index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine,
> converters, true_values, false_values, skipinitialspace, skiprows,
> nrows, na_values, keep_default_na, na_filter, verbose,
> skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col,
> date_parser, dayfirst, iterator, chunksize, compression, thousands,
> decimal, lineterminator, quotechar, quoting, escapechar, comment,
> encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines,
> skipfooter, doublequote, delim_whitespace, low_memory, memory_map,
> float_precision)
> 676 skip_blank_lines=skip_blank_lines)
> 677
> --> 678 return _read(filepath_or_buffer, kwds)
> 679
> 680 parser_f.__name__ = name
>
> /home/nbuser/anaconda2_501/lib/python2.7/site-packages/pandas/io/parsers.pyc
> in _read(filepath_or_buffer, kwds)
> 438
> 439 # Create the parser.
> --> 440 parser = TextFileReader(filepath_or_buffer, **kwds)
> 441
> 442 if chunksize or iterator:
>
> /home/nbuser/anaconda2_501/lib/python2.7/site-packages/pandas/io/parsers.pyc
> in __init__(self, f, engine, **kwds)
> 785 self.options['has_index_names'] = kwds['has_index_names']
> 786
> --> 787 self._make_engine(self.engine)
> 788
> 789 def close(self):
>
> /home/nbuser/anaconda2_501/lib/python2.7/site-packages/pandas/io/parsers.pyc
> in _make_engine(self, engine) 1012 def _make_engine(self,
> engine='c'): 1013 if engine == 'c':
> -> 1014 self._engine = CParserWrapper(self.f, **self.options) 1015 else: 1016 if engine == 'python':
>
> /home/nbuser/anaconda2_501/lib/python2.7/site-packages/pandas/io/parsers.pyc
> in __init__(self, src, **kwds) 1706 kwds['usecols'] =
> self.usecols 1707
> -> 1708 self._reader = parsers.TextReader(src, **kwds) 1709 1710 passed_names = self.names is None
>
> pandas/_libs/parsers.pyx in
> pandas._libs.parsers.TextReader.__cinit__()
>
> pandas/_libs/parsers.pyx in
> pandas._libs.parsers.TextReader._setup_parser_source()
>
> IOError: File Sample.csv does not exist
如何读取上传的文件?这个问题困扰了我好久!!
另外,通过界面上传后上传到哪里?