1

My code is:-

   import pandas as pd
   import numpy as np
   import plotly.express as px
   import plotly.graph_objects as go
   import plotly.io as pio
   import os

    print(os.getcwd())
    os.chdir("//Users//apple//Desktop//Employedata")
    print(os.getcwd())
   pio.templates.default = "plotly_white"
   data = pd.read_csv("/Users/apple/Desktop/Employedata/EmployeeSampleData.csv")
   print(data)

It is showing error as follows in the console:-

    /Users/apple/PycharmProjects/pythonProject4/venv/bin/python /Users/apple     /PycharmProjects/pythonProject4/main.py 
    /Users/apple/PycharmProjects/pythonProject4
    /Users/apple/Desktop/Employedata
   
   Traceback (most recent call last):
   File "/Users/apple/PycharmProjects/pythonProject4/main.py", line 12, in <module>
   data = pd.read_csv("/Users/apple/Desktop/Employedata/EmployeeSampleData.csv")
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.11/site-packages/pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
       ^^^^^^^^^^^^^^^^^^^^^
    File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.11/site-packages/pandas/util/_decorators.py", line 331, in wrapper
      return func(*args, **kwargs)
       ^^^^^^^^^^^^^^^^^^^^^
     File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 950, in read_csv
      return _read(filepath_or_buffer, kwds)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 605, in _read
        parser = TextFileReader(filepath_or_buffer, **kwds)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 1442, in __init__
       self._engine = self._make_engine(f, self.engine)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 1753, in _make_engine
     return mapping[engine](f, **self.options)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.11/site-packages/pandas/io/parsers/c_parser_wrapper.py", line 79, in __init__
     self._reader = parsers.TextReader(src, **kwds)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "pandas/_libs/parsers.pyx", line 547, in     pandas._libs.parsers.TextReader.__cinit__
     File "pandas/_libs/parsers.pyx", line 636, in       pandas._libs.parsers.TextReader._get_header
    File "pandas/_libs/parsers.pyx", line 852, in pandas._libs.parsers.TextReader._tokenize_rows
    File "pandas/_libs/parsers.pyx", line 1965, in pandas._libs.parsers.raise_parser_error
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 14194:    invalid start byte

   Process finished with exit code 1

I unable to resolve it. I do not know what to do. I am using PyCharm 2022.2.3 with Python 3.11. Also, using mac. If you need further information, please do kindly message me

Thanks.

askit
  • 205
  • 4
  • 21
  • Can you attach a file sample? – Sala Jan 10 '23 at 13:00
  • You probably need to specify the `encoding` parameter on the `read_csv` function – Sala Jan 10 '23 at 13:02
  • 3
    Have you tried googling your error ? It seems like there already is a question about this one : https://stackoverflow.com/questions/48067514/utf-8-codec-cant-decode-byte-0xa0-in-position-4276-invalid-start-byte – l -_- l Jan 10 '23 at 13:03
  • 2
    Does this answer your question? ['utf-8' codec can't decode byte 0xa0 in position 4276: invalid start byte](https://stackoverflow.com/questions/48067514/utf-8-codec-cant-decode-byte-0xa0-in-position-4276-invalid-start-byte) – DataJanitor Jan 10 '23 at 13:14
  • @Sala - how to specify encoding parameter. Using mac with pycharm as specified above – askit Jan 11 '23 at 04:41
  • `pd.read_csv(filename, encoding='utf8')` but i do not know how your file is encoded – Sala Jan 11 '23 at 13:42
  • @Sala - how to tell you that ? – askit Jan 11 '23 at 13:49
  • @I and @Jan - it seems to solve the issue - the correct answer is - data = pd.read_csv("/Users/apple/Desktop/Employedata/EmployeeSampleData.csv",encoding='windows-1252') – askit Jan 12 '23 at 04:06

0 Answers0