Cannot Convert Local CSV to dataframe in Pandas on Spyder

Question:

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:Testtest.txt")

# output the dataframe
print(df)

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

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

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

  File C:Program FilesSpyderpkgspandasioparsersreaders.py:912 in read_csv
    return _read(filepath_or_buffer, kwds)

  File C:Program FilesSpyderpkgspandasioparsersreaders.py:577 in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)

  File C:Program FilesSpyderpkgspandasioparsersreaders.py:1407 in __init__
    self._engine = self._make_engine(f, self.engine)

  File C:Program FilesSpyderpkgspandasioparsersreaders.py:1661 in _make_engine
    self.handles = get_handle(

  File C:Program FilesSpyderpkgspandasiocommon.py:859 in get_handle
    handle = open(

OSError: [Errno 22] Invalid argument: 'C:\Testtest.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.

Asked By: Englishman Bob

||

Answers:

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:Testtest.txt'

Answered By: Eyad Ahmed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.