Forums

Unable to Load a CSV File

Very new to Python.

I upload a file called "Data.csv" in files.

import os 
os.getcwd()

returns

'/home/likepigs'

import pandas as pd
df = pd.read_csv("/home/likepigs/Data.csv")

returns

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-52-8f2cabf1e99b> in <module>()
      1 # load data file
      2 import pandas as pd
----> 3 df = pd.read_csv("/home/likepigs/Data.csv")

/usr/local/lib/python3.5/dist-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, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    560                     skip_blank_lines=skip_blank_lines)
    561 
--> 562         return _read(filepath_or_buffer, kwds)
    563 
    564     parser_f.__name__ = name

/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    323         return parser
    324 
--> 325     return parser.read()
    326 
    327 _parser_defaults = {

/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in read(self, nrows)
    813                 raise ValueError('skip_footer not supported for iteration')
    814 
--> 815         ret = self._engine.read(nrows)
    816 
    817         if self.options.get('as_recarray'):

/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in read(self, nrows)
   1312     def read(self, nrows=None):
   1313         try:
-> 1314             data = self._reader.read(nrows)
   1315         except StopIteration:
   1316             if self._first_chunk:

pandas/parser.pyx in pandas.parser.TextReader.read (pandas/parser.c:8748)()

pandas/parser.pyx in pandas.parser.TextReader._read_low_memory (pandas/parser.c:9003)()

pandas/parser.pyx in pandas.parser.TextReader._read_rows (pandas/parser.c:10022)()

pandas/parser.pyx in pandas.parser.TextReader._convert_column_data (pandas/parser.c:11397)()

pandas/parser.pyx in pandas.parser.TextReader._convert_tokens (pandas/parser.c:12302)()

pandas/parser.pyx in pandas.parser.TextReader._convert_with_dtype (pandas/parser.c:13740)()

pandas/parser.pyx in pandas.parser.TextReader._string_convert (pandas/parser.c:13983)()

pandas/parser.pyx in pandas.parser._string_box_utf8 (pandas/parser.c:19298)()

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

[edited by admin: formatting]

that sounds like your csv file is corrupted?

I can open the cvs file without a problem, even from python anywhere

Your csv is not valid utf-8. Either find the character that is not proper utf-8 and replace it, or pass the correct encoding into read_csv.