VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Inverted comma literal (https://forums.vandyke.com/showthread.php?t=13604)

slouw 06-27-2019 02:04 AM

Inverted comma literal
 
Related to this forum post
I have a script that turns off logging, changes the log file name and turns on logging again.
In this script is the fragment below to replace certain defined characters in a string with a "_".
The string is the new logfile name.
The illegal characters are those that cannot be included in a filename.
All works well accept that this script fragment does not remove a double quote
"
resulting in an error.
Any ideas how I can represent the double quote as a literal so code fragment will do what is intended?

With much gratitude...

Code:

IllegalChars ="|/\<>:*"""
## Problem here is that each illegal char is searched for only once.
## When 2 senak in we have a problem
for char in IllegalChars:
    if char in CommandStr:
        ## .replace method should replace ALL occurrences.
        ## Seems this is not happening
        CommandStr = CommandStr.replace(char, "_")
        writeB("00380 **PRESENT**                :" + CommandStr + "<>" + "  " + char)
        ## crt.Dialog.MessageBox("00380 **PRESENT** :" + CommandStr + "<>" + "  " + char)
    else:
        ## crt.Dialog.MessageBox("00390 No          :" + CommandStr + "<>" + "  " + char)
        writeB("00390 No                          :" + CommandStr + "<>" + "  " + char)


gregg 07-06-2019 10:47 AM

Try either:

Code:

IllegalChars ='|/\\<>:*"'
Or:

Code:

IllegalChars ="|/\\<>:*\""
In Python, certain string literals need to be escaped, such as \ and "

If you wrap the string in double quotes (") then you need to escape double quotes (\"), however, if you wrap a string in single quotes ('), then you don't need to escape double quotes, but would need to escape single quotes (\')

see: https://docs.python.org/2.0/ref/strings.html

slouw 07-07-2019 08:49 PM

Will do thanks fro reply....


All times are GMT -6. The time now is 08:56 PM.