在PyCharm中运行测试时出现UnicodeDecodeError

时间:2018-12-03 09:57:54

标签: python-3.x pycharm python-unicode

我的项目中有一个测试,涉及打开和读取文件。代码块基本上是这样的:

import pandas as pd

with open('path/to/the/file') as file:
  df = pd.read_csv(file, comment='#')

在PyCharm中运行测试时,出现此错误:

  File "/Users/andrea.marchini/.local/share/virtualenvs/customer-ontology-OqrlJ3HG/lib/python3.5/site-packages/pandas/io/parsers.py", line 655, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Users/andrea.marchini/.local/share/virtualenvs/customer-ontology-OqrlJ3HG/lib/python3.5/site-packages/pandas/io/parsers.py", line 411, in _read
    data = parser.read(nrows)
  File "/Users/andrea.marchini/.local/share/virtualenvs/customer-ontology-OqrlJ3HG/lib/python3.5/site-packages/pandas/io/parsers.py", line 1005, in read
    ret = self._engine.read(nrows)
  File "/Users/andrea.marchini/.local/share/virtualenvs/customer-ontology-OqrlJ3HG/lib/python3.5/site-packages/pandas/io/parsers.py", line 1748, in read
    data = self._reader.read(nrows)
  File "pandas/_libs/parsers.pyx", line 890, in pandas._libs.parsers.TextReader.read (pandas/_libs/parsers.c:10862)
  File "pandas/_libs/parsers.pyx", line 912, in pandas._libs.parsers.TextReader._read_low_memory (pandas/_libs/parsers.c:11138)
  File "pandas/_libs/parsers.pyx", line 966, in pandas._libs.parsers.TextReader._read_rows (pandas/_libs/parsers.c:11884)
  File "pandas/_libs/parsers.pyx", line 953, in pandas._libs.parsers.TextReader._tokenize_rows (pandas/_libs/parsers.c:11755)
  File "pandas/_libs/parsers.pyx", line 2173, in pandas._libs.parsers.raise_parser_error (pandas/_libs/parsers.c:28589)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 202538: ordinal not in range(128)

无论如何,如果我从python控制台运行相同的代码,一切都会很好。

1 个答案:

答案 0 :(得分:1)

将此行添加到.bash_profile文件中即可解决此问题

if [ -z "$LANG" ]; then export LANG="$(defaults read -g AppleLocale | sed 's/@.*$//g').UTF-8"; fi