【发布时间】:2019-01-06 23:12:35
【问题描述】:
我正在尝试编写一个用于在命令行上解析 Excel 的工具;它适用于旧版本的 Python 和 pandas,但不适用于新版本。
Python 2 和 Python 3 之间的sys.stdin 似乎有所不同,但我无法继续。
$ conda create -n py2 python=2.7 pandas=0.17.1 xlrd
$ source activate py2
(py2) $ cat data.xlsx | python -c "import pandas as pd; import sys; df = pd.read_excel(sys.stdin); print(df.head())"
x y
0 1 2
1 10 100
(py2) $ source deactivate
$ conda create -n py3 python=3.6 pandas=0.23.3 xlrd
$ source activate py3
(py3) $ cat data.xlsx | python -c "import pandas as pd; import sys; df = pd.read_excel(sys.stdin); print(df.head())"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/bilow/anaconda3/envs/py3/lib/python3.6/site-packages/pandas/util/_decorators.py", line 178, in wrapper
return func(*args, **kwargs)
File "/Users/bilow/anaconda3/envs/py3/lib/python3.6/site-packages/pandas/util/_decorators.py", line 178, in wrapper
return func(*args, **kwargs)
File "/Users/bilow/anaconda3/envs/py3/lib/python3.6/site-packages/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile(io, engine=engine)
File "/Users/bilow/anaconda3/envs/py3/lib/python3.6/site-packages/pandas/io/excel.py", line 391, in __init__
data = io.read()
File "/Users/bilow/anaconda3/envs/py3/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 15-16: invalid continuation byte
(py3) $ source deactivate
【问题讨论】:
标签: python excel python-3.x pandas python-2.x