invalid escape sequence when open csv file in julia

Viewed 1958

when open the CSV file in julia it give "Invalid Escape Sequence" error.Invalid Escape Sequence

julia> using CSV

julia> wikiEVDraw = CSV.read("D:\Online_Courses\...\wikipediaEVDraw.csv")
ERROR: syntax: invalid escape sequence
1 Answers

The problem is that Julia interprets \O, \. and \w in this example as an escape sequence, but since neither of those is a valid escape sequence, that is why you get a syntax error with that error message, ie:

julia> path = "D:\Online_Courses\...\wikipediaEVDraw.csv"
ERROR: syntax: invalid escape sequence
Stacktrace:
 [1] top-level scope at REPL[32]:0

Escape \ with \\ because you want to interpret \ as a literal backslash, not as the beginning of an escape sequence:

julia> path = "D:\\Online_Courses\\...\\wikipediaEVDraw.csv"
"D:\\Online_Courses\\...\\wikipediaEVDraw.csv"

And then this will work:

julia> wikiEVDraw = CSV.read("D:\\Online_Courses\\...\\wikipediaEVDraw.csv")

Or use @raw_str string macro to do it automatically:

julia> path = raw"D:\Online_Courses\...\wikipediaEVDraw.csv"
"D:\\Online_Courses\\...\\wikipediaEVDraw.csv"

So you can do:

julia> wikiEVDraw = CSV.read(raw"D:\Online_Courses\...\wikipediaEVDraw.csv")

Or you can also use forward slashes / for paths as in Linux or MacOS instead of the backslash \ for windows specific paths inside strings:

julia> wikiEVDraw = CSV.read("D:/Online_Courses/.../wikipediaEVDraw.csv")
Related