gpt4 book ai didi

python - 如何将 CSV 文件读入 Jupyter Notebook

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:02 26 4
gpt4 key购买 nike

我在将 CSV 文件读入 Jupyter Notebook 时遇到问题。这是代码:

import pandas as pd
mpg = pd.read_csv('C:/Users/Ajibola/Documents/mpg.csv')
mpg.head()

这是我得到的错误:

File "<ipython-input-138-844bace16611>", line 1
mpg = pd.read_csv('C:\Users\Ajibola\Documents\mpg.csv')
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

在 PATH 前面加上 r 后,我得到了错误:

---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-140-a1289650ba91> in <module>
----> 1 mpg = pd.read_csv(r'C:\Users\Ajibola\Documents\mpg.csv')
2 mpg.head()

~\Anaconda3\lib\site-packages\pandas\io\parsers.py 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, skipfooter, 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, doublequote, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
700 skip_blank_lines=skip_blank_lines)
701
--> 702 return _read(filepath_or_buffer, kwds)
703
704 parser_f.__name__ = name

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
427
428 # Create the parser.
--> 429 parser = TextFileReader(filepath_or_buffer, **kwds)
430
431 if chunksize or iterator:

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
893 self.options['has_index_names'] = kwds['has_index_names']
894
--> 895 self._make_engine(self.engine)
896
897 def close(self):

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
1120 def _make_engine(self, engine='c'):
1121 if engine == 'c':
-> 1122 self._engine = CParserWrapper(self.f, **self.options)
1123 else:
1124 if engine == 'python':

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
1851 kwds['usecols'] = self.usecols
1852
-> 1853 self._reader = parsers.TextReader(src, **kwds)
1854 self.unnamed_cols = self._reader.unnamed_cols
1855

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._get_header()

UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: invalid continuation byte

我在社区中跑遍了相关问题和答案,但没有取得任何进展。非常感谢您的回答。

最佳答案

在与您的代码相同的文件夹中创建您的 .csv 文件。这会起作用

import pandas as pd
data = pd.read_csv('data.csv')
print(data)

关于python - 如何将 CSV 文件读入 Jupyter Notebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com