rawstring

Python - Raw String Literals

邮差的信 提交于 2019-12-01 16:31:29
问题 I don't understand how raw string literals work. I know that when using r it ignores all specials, like when doing \n it treats it as \n and not as a new line. but then I tried to do this: x = r'\' and it said SyntaxError: EOL while scanning string literal and not '\' why? did I understanded it correctly? and also what is the explanation for this : print r'\\' # gives '\\' print r'\\\' # gives SyntaxError 回答1: The only way to put in a single quote into a string started with a single quote is

Raw string literals and file codification

旧时模样 提交于 2019-12-01 07:09:04
C++11 introduced the raw string literals which can be pretty useful to represent quoted strings, literals with lots of special symbols like windows file paths, regex expressions etc... std::string path = R"(C:\teamwork\new_project\project1)"; // no tab nor newline! std::string quoted = R"("quoted string")"; std::string expression = R"([\w]+[ ]+)"; This raw string literals can also be combined with encoding prefixes ( u8 , u , U , or L ), but, when no encoding prefix is specified, does the file encoding matters?, lets suppose that I have this code: auto message = R"(Pick up a card)"; // raw

In context of Python Raw string

吃可爱长大的小学妹 提交于 2019-11-30 15:04:27
问题 My Python version is: ~$ python --version Python 2.6.6 I tried following in Python (I wants to show all): 1: \ use as escape sequence >>> str('Let\'s Python') "Let's Python" 2: \ use as escape sequence >>> 'Let\'s Python' "Let's Python" 3: str() and print as value not type >>> print 'Let\'s Python' Let's Python 4: its Python a raw string >>> repr('Let\'s Python') '"Let\'s Python"' [ QUESTION ] 5: Python raw string >>> print r'Let\'s Python' Let\'s Python 6: This, I do not understand

In context of Python Raw string

百般思念 提交于 2019-11-30 12:45:53
My Python version is: ~$ python --version Python 2.6.6 I tried following in Python (I wants to show all): 1: \ use as escape sequence >>> str('Let\'s Python') "Let's Python" 2: \ use as escape sequence >>> 'Let\'s Python' "Let's Python" 3: str() and print as value not type >>> print 'Let\'s Python' Let's Python 4: its Python a raw string >>> repr('Let\'s Python') '"Let\'s Python"' [ QUESTION ] 5: Python raw string >>> print r'Let\'s Python' Let\'s Python 6: This, I do not understand followings: >>> r'Let\'s Python' "Let\\'s Python" >>> r'\\' '\\\\' Why \\ ? Why output in 5 and 6 are different?

How to format raw string with different expressions inside?

元气小坏坏 提交于 2019-11-30 07:27:35
Let's say, we want to catch something with regex, using rawstring to define the pattern, which pattern has repeating elements, and variables inside. And we also want to use the format() string formatting form. How to do this? import re text = '"""!some text' re.findall(r'"{3}{symbol}some\stext'.format(symbol='!'), text) But this line leads us to an IndexError : # IndexError: tuple index out of range So, my question is: how to format a raw string if it has formatting curly-braces expression, and repeating curly-braces expression inside? Thanks in advance! Escape the curly brackets with curly

How to match a new line character in Python raw string

扶醉桌前 提交于 2019-11-30 05:50:54
I got a little confused about Python raw string. I know that if we use raw string, then it will treat '\' as a normal backslash (ex. r'\n' would be '\' and 'n'). However, I was wondering what if I want to match a new line character in raw string. I tried r'\n', but it didn't work. Anybody has some good idea about this? In a regular expression, you need to specify that you're in multiline mode: >>> import re >>> s = """cat ... dog""" >>> >>> re.match(r'cat\ndog',s,re.M) <_sre.SRE_Match object at 0xcb7c8> Notice that re translates the \n (raw string) into newline. As you indicated in your

How to format raw string with different expressions inside?

为君一笑 提交于 2019-11-29 09:37:41
问题 Let's say, we want to catch something with regex, using rawstring to define the pattern, which pattern has repeating elements, and variables inside. And we also want to use the format() string formatting form. How to do this? import re text = '"""!some text' re.findall(r'"{3}{symbol}some\stext'.format(symbol='!'), text) But this line leads us to an IndexError : # IndexError: tuple index out of range So, my question is: how to format a raw string if it has formatting curly-braces expression,

Does clojure have raw string?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 08:22:33
In Python, I can prefix an r to a string literal (raw string) to tell the interpreter not translate special characters in the string: >>> r"abc\nsdf#$%\^" r"abc\nsdf#$%\^" Is there a way to do the same thing in Clojure? Arthur Ulfeldt Clojure strings are Java strings and the reader does not add anything significant to their interpretation. The reader page just says "standard Java escape characters are supported." You can escape the \ though: user> (print "abc\\nsdf#$%\\^") abc\nsdf#$%\^ This only affect string literals read by the reader, so if you read strings from a file the reader never

How to create raw string from string variable in python?

不问归期 提交于 2019-11-27 23:59:05
You create raw string from a string this way: test_file=open(r'c:\Python27\test.txt','r') How do you create a raw variable from a string variable, such as path = 'c:\Python27\test.txt' test_file=open(rpath,'r') Because I have a file path: file_path = "C:\Users\b_zz\Desktop\my_file" When I do: data_list = open(os.path.expandvars(file_path),"r").readlines() I get: Traceback (most recent call last): File "<pyshell#32>", line 1, in <module> scheduled_data_list = open(os.path.expandvars(file_path),"r").readlines() IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\x08_zz\\Desktop\\my

How to match a new line character in Python raw string

断了今生、忘了曾经 提交于 2019-11-27 21:09:10
问题 I got a little confused about Python raw string. I know that if we use raw string, then it will treat '\' as a normal backslash (ex. r'\n' would be '\' and 'n'). However, I was wondering what if I want to match a new line character in raw string. I tried r'\n', but it didn't work. Anybody has some good idea about this? 回答1: In a regular expression, you need to specify that you're in multiline mode: >>> import re >>> s = """cat ... dog""" >>> >>> re.match(r'cat\ndog',s,re.M) <_sre.SRE_Match