This article mainly describes the Python raw string and the Unicode string operator usage, in combination with the instance form of Python for the original character and Unicode characters of the operator usage, the need for friends can refer to the following
This example describes the Python raw string and the Unicode string operator usage. Share to everyone for your reference, as follows:
#coding =utf8 ' "in the original string, all strings are used directly as literal meanings, and no special or non-printable characters are escaped. A regular expression is a string of words that admonish the search match, consisting of special symbols representing characters, groupings, matching information, variable names, character classes, and so on. Before the original string immediately precedes the first quotation mark, an R or R letter is required to indicate that the character is the original string. The original string and the normal string have this almost identical syntax. The Unicode string operators, uppercase U and lowercase u are introduced together in Python1.6 and Unicode strings. It is used to convert a standard string or a string containing Unicode characters into a full Unicode string object. The string method and the regular expression engine also support Unicode. The Unicode operator can also accept the original Unicode string, as long as the Unicode operator is concatenated with the original string operator. Note: The Unicode operator must appear before the original string operator. ' Import re# primitive operator function def originaloper (): #不是原始字符表示换行符 print "\ n" #是原始字符表示 \ n Print r "\ n" try: #对文件路径加r, do not need to escape \ #文件路径中表示斜线 Testfile=open (r "E:\PythonDemo\CorePythonBook2\output", "R") for line in Testfile.readlines (): #通过格式化字 The string #优先使用repr () function is converted to the original character line= "%r"%line #对匹配模式进行加r处理 #匹配的字符转换为原始字符 m=re.search (r "\\[rn]", line) #字符串中是否包含匹配字符 #如果包含输出该字符串 if M isn't none:print line except Exception,e:print e finally:te Stfile.close () #Unicode操作符函数def unicodeoper (): Print u "abc" Print U "\u1234" Print U "abc\u1234\n" Print ur "hello\nworld !" #调用函数#输出操作原始字符结果originalOper () #输出Unicode字符串unicodeOper ()
The results of the operation are as follows: