How to do escape_string

I am trying to use escape_string because as per documentation, “Backslashes ( \ ) are escaped with a double-backslash ( "\\" )”.

This is what I tried:

mypath1 = escape_string("C:\Users\User\Dropbox\codes\JULIA\stock1\")

The error message I get is

ERROR: LoadError: syntax: invalid escape sequence.

I am not sure what I did wrong.

1 Like

So, I’m not sure escape_string is what you need. You could try to use raw strings.

mypath1 = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\"

Oops! That doesn’t work. I forgot that one needs to add a double slash at the end.

julia> mypath = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"

Alternatively, you could just do the escaping manually…

julia> mypath = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"
1 Like

I think you’re misunderstanding escape_string — it is not to help you type the literal string, but rather it is there to help you add backslashes to an existing string, primarily for printing.

So, when you type this path as a literal string, you still need the extra backslashes:

julia> mypath = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"

Note that it is displayed between "..." as you would type it, with the escaped backslashes, but these are not actually stored in the string, as you can see if you print it:

julia> println("This is the mypath string: ", mypath)
This is the mypath string: C:\Users\User\Dropbox\codes\JULIA\stock1\

You can also enter the same string using raw"..." if you want to avoid having to type so many backslashes, although you still to escape the final backslash:

julia> mypath2 = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"

julia> mypath2 == mypath
true

As mentioned above, escape_string actually adds the backslash escapes into the string, which looks a bit weird if you display it because \\ is displayed escaped as \\\\:

julia> escape_string(mypath)
"C:\\\\Users\\\\User\\\\Dropbox\\\\codes\\\\JULIA\\\\stock1\\\\"

The reason for escape_string is more apparent if you print it:

julia> println("This is the mypath string WITH ESCAPES: ", escape_string(mypath))
This is the mypath string WITH ESCAPES: C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\

Some form of escaping is used for literal strings in every programming language, because of the inevitable need to distinguish between language syntax and the contents of the string for things like string delimiters. xkcd:

4 Likes

Thanks for the lenghty explanation. I actually started with

mypath = “C:\Users\User\Dropbox\codes\JULIA\stock1\”`

but I recognize the user may not always add the extra backslash

My intent is this
mydf = DataFrame(CSV.File(mypath))

given if

mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\"

Assuming mypath is not changed, how?
If I have to use raw, I have to add that extra backslash to mypath

but
mypath2 = mypath*"\"

doesn’t work, and adding raw in between too somehow doesn’t work. I tried parsing, but that made it worse, I am so new to this :sweat_smile:

I think that CSV.File requires a filepath, not a directory. So, perhaps you could use the filename?

myfile = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\myfile.csv"
mydf = DataFrame(CSV.File(myfile))

In this case, since you don’t have a trailing slash, raw string works well…

myfile = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\myfile.csv"
mydf = DataFrame(CSV.File(myfile))

Thanks!

mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
myfile = Meta.parse(raw*mypath)
mydf = DataFrame(CSV.File(myfile))

May I ask, how do I correct the above?

mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
myfile = Meta.parse("raw"*mypath)
mydf = DataFrame(CSV.File(myfile))

The above doesn’t work as well

myfile = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
mydf = DataFrame(CSV.File(myfile))

Why do you think you need Meta.parse?

1 Like

I am assuming anything that is not a variable need to be in quotation if it is in parse.

This meta.parse works

ex1 = Meta.parse("DataFrame(yahoo("*b*", YahooOpt(period1 = startday, period2 = endday)))") 
mydf1 = eval(ex1)

Should be

mypath = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
mydf = DataFrame(CSV.File(myfile))

You need raw (or backslash escapes) when you enter the string in your code.

1 Like

I think you’re over thinking it. Do the 2 lines above work?

1 Like

Thanks. If
mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
remains, how do I mix this raw?

Who is the “user” here? Are you typing the path, or are you reading it from some external source?

(The above won’t work because mypath = "..." is Julia code and needs to follow Julia syntax — you need to escape backslashes or use raw"...". I’m not sure you know what escaping actually means yet?)

1 Like

To assign that string to a variable in Julia, you have two options. Either you manually escape each slash by doubling it…

mypath = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\TRAINING\\IBM.csv"

or you use raw string…

mypath = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"

In Julia string literals, the backslash has special meaning so that you can add non-printable characters (such as the newline) or, include the double quote.

julia> print("The sun came over the horizon.\nHe said: \"Beautiful!\"")
The sun came over the horizon.
He said: "Beautiful!"

This is why using slash in a string literal is complex. So, to actually include a slash in your output string, you have to double it.

Anyway, I’d avoid eval and Meta.parse – it’s exceptionally rare you’d ever need to use them. I’ve been using Julia for years in some complicated works and have found only a few cases where Meta.parse and eval were indicated.

2 Likes

The plan is it will be an external source in the future.

Maybe I have not intuitively understand what escaping is.
But the way I understand it, I do need the double backslash to make it a real backslash.

If the external source is not Julia code (e.g. you are reading the filename using read or from a file-picker prompt), then the user won’t need extra backslashes. Escaping is only needed when strings are entered literally in Julia code.

in Julia code. The point is that when you have a string "...." in Julia code (a string literal), this is only a representation of the actual contents of the string, and is not the same as the string itself as it is stored in memory. A backslash in the underlying string is represented in Julia code by a string literal with an escaped (backslashed) backslash (a double backslash).

In the same way, if you want a string with a quotation mark, you need to represent it in Julia code with an escaped backslash: "a quote \" in a string". This is, in some sense, the origin of escaping: if you didn’t mark (escape) the quotation mark in some special way, Julia couldn’t distinguish it from the end of the string. But then once you settle on \" as the way to escape a quote ", then you need a way to escape the backslash itself.

Again, this is not specific to Julia. Every programming language has some version of string escaping.

3 Likes

Thanks! I will try reading the path from a file.

1 Like