Search code examples
pythonpandasdataframe

Cannot Convert Local CSV to dataframe in Pandas on Spyder


Hello I am learning Python using Spyder (Python 3.8.10 64-bit | Qt 5.15.2 | PyQt5 5.15.10 | Windows 10 (AMD64). I am unable to convert a local copy of a csv into a dataframe using pandas

# import pandas module
import pandas as pd

# making dataframe
df = pd.read_csv("C:\Test\test.txt")

# output the dataframe
print(df)

i have verified folder exists and is contain file. Spyder returns

df = pd.read_csv("C:\Test\test.txt")
Traceback (most recent call last):

  Cell In[114], line 1
    df = pd.read_csv("C:\Test\test.txt")

  File C:\Program Files\Spyder\pkgs\pandas\io\parsers\readers.py:912 in read_csv
    return _read(filepath_or_buffer, kwds)

  File C:\Program Files\Spyder\pkgs\pandas\io\parsers\readers.py:577 in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)

  File C:\Program Files\Spyder\pkgs\pandas\io\parsers\readers.py:1407 in __init__
    self._engine = self._make_engine(f, self.engine)

  File C:\Program Files\Spyder\pkgs\pandas\io\parsers\readers.py:1661 in _make_engine
    self.handles = get_handle(

  File C:\Program Files\Spyder\pkgs\pandas\io\common.py:859 in get_handle
    handle = open(

OSError: [Errno 22] Invalid argument: 'C:\\Test\test.txt'

What am I doing wrong? Note: I am barely a code monkey.

I have tried:


!pip install ipython-sql

prior to the commands above. Spyder spits out some text beginning with

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: ipython-sql in c:

but is otherwise stable.


Solution

  • Backslash is a special character in Python strings, it needs to be "escaped", the backslash itself is used to escape characters, so you need double backslashes in the path, because Windows uses backslashes as path separator

    So use this instead 'C:\\Test\\test.txt'

    You can also use a "raw" string, which avoids the need for escaping the backslash r'C:\Test\test.txt'