在 Julia 中打开 csv 文件时转义序列无效 [英] invalid escape sequence when open csv file in julia
问题描述
当在 julia 中打开 CSV 文件时,它会给出 Invalid Escape Sequence" 错误.无效的转义序列
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
推荐答案
问题是Julia解释了O
、.
和w
在本例中作为 转义序列,但由于这些都不是有效的转义序列,这就是为什么您会收到带有该错误消息的语法错误,即:
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"
然后这将起作用:
julia> wikiEVDraw = CSV.read("D:\Online_Courses\...\wikipediaEVDraw.csv")
或者使用 @raw_str
字符串宏自动完成:
Or use @raw_str
string macro to do it automatically:
julia> path = raw"D:Online_Courses...wikipediaEVDraw.csv"
"D:\Online_Courses\...\wikipediaEVDraw.csv"
所以你可以这样做:
julia> wikiEVDraw = CSV.read(raw"D:Online_Courses...wikipediaEVDraw.csv")
或者您也可以使用正斜杠 /
表示 Linux 或 MacOS 中的路径,而不是使用反斜杠 表示字符串中的 Windows 特定路径:
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")
这篇关于在 Julia 中打开 csv 文件时转义序列无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!