Python Forum

Full Version: csv error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Any idea what could be causing this error?
def read_to_df(file_path):
    """Read on-disk data and return a dataframe."""
    
    ##read_file = pd.io.parsers.read_csv(file_path)
    df = pd.read_csv(file_path, sep=',')

        
    return read_file

test = read_to_df("C:\Users\Kathleen\Documents\Mike\Emeritus\csvtest.csv")
print(test)
This is the error
Error:
File "<ipython-input-14-7515d68addf8>", line 20 test = read_to_df("C:\Users\Kathleen\Documents\Mike\Emeritus\csvtest.csv") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character., i.e. in combination with some chars (e.g. u) you get escape sequence. Check https://docs.python.org/3/reference/lexi...l#literals

either use forward slash, not backslash or use raw string, e.g.
test = read_to_df(r"C:\Users\Kathleen\Documents\Mike\Emeritus\csvtest.csv")