将 CSV 导入 Python

Importing CSV to Python

我是 Python 的新手,不明白我收到的错误。我正在尝试将 csv 文件导入 python,但遇到了问题。我已经能够毫无问题地导入保存在同一位置的其他 csv 文件,但我尝试导入的文件却给我一条错误消息。非常感谢任何帮助。

这是我用来尝试导入的代码和我收到的错误消息:

import pandas as pd

stats = pd.read_csv('C:\Users\ao322\Downloads\DSI_kickstarterscrape_dataset.csv')


UnicodeDecodeError                        Traceback (most recent call last)
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._string_convert()

pandas\_libs\parsers.pyx in pandas._libs.parsers._string_box_utf8()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 12: invalid start byte

During handling of the above exception, another exception occurred:

UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-7-04a025fb428b> in <module>
----> 1 stats = pd.read_csv('C:\Users\ao322\Downloads\DSI_kickstarterscrape_dataset.csv')

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    684     )
    685 
--> 686     return _read(filepath_or_buffer, kwds)
    687 
    688 

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    456 
    457     try:
--> 458         data = parser.read(nrows)
    459     finally:
    460         parser.close()

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in read(self, nrows)
   1194     def read(self, nrows=None):
   1195         nrows = _validate_integer("nrows", nrows)
-> 1196         ret = self._engine.read(nrows)
   1197 
   1198         # May alter columns / col_dict

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in read(self, nrows)
   2153     def read(self, nrows=None):
   2154         try:
-> 2155             data = self._reader.read(nrows)
   2156         except StopIteration:
   2157             if self._first_chunk:

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.read()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._read_rows()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_column_data()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._string_convert()

pandas\_libs\parsers.pyx in pandas._libs.parsers._string_box_utf8()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 12: invalid start byte

​

尝试提供编码和检查

import pandas as pd

stats = pd.read_csv('C:\Users\ao322\Downloads\DSI_kickstarterscrape_dataset.csv',encoding='utf-8')

尝试提供编码cp1252并检查

import pandas as pd

stats = pd.read_csv('C:\Users\ao322\Downloads\DSI_kickstarterscrape_dataset.csv',encoding='cp1252'')

在文本编辑器中打开 csv,最好是 VS 代码或 Sublime 文本,然后使用编码保存。

Click File -> Save with encoding -> UTF-8

然后

import pandas as pd
data = pd.read_csv('yourFile.csv', encoding='utf-8')

你也可以这样做

import pandas as pd
df = pd.read_csv('yourFile.csv', engine='python')