Python原始字串與Unicode字串操作符用法執行個體分析,pythonunicode

來源:互聯網
上載者:User

Python原始字串與Unicode字串操作符用法執行個體分析,pythonunicode

本文執行個體講述了Python原始字串與Unicode字串操作符用法。分享給大家供大家參考,具體如下:

#coding=utf8'''''在原始字串裡,所有的字串都是直接按照字面的意思來使用,沒有轉義特殊或不能列印的字元。Regex是一些告誡搜尋匹配方式的字串,通過是由代表字元、分組、匹配資訊、變數名、字元類等的特殊符號組成。在原始字串緊靠第一個引號前,需要加上r或R字母,來表示該字元是原始字串。原始字串和一般字元串有這幾乎完全相同的文法。Unicode字串操作符,大寫的U和小寫u是在Python1.6中和Unicode字串一起被引入的。它用來把標準字串或者包含Unicode字元的字串轉換成完全的Unicode字串對象。字串方法和Regex引擎也支援Unicode。Unicode操作符也可以接受原始Unicode字串,只要將Unicode操作符和原始字串操作符串連在一起。注意:Unicode操作符必須出現在原始字串操作符前面。'''import re#原始操作符函數def originalOper():  #不是原始字元表示分行符號  print "\n"  #是原始字元表示\n  print r"\n"  try:    #對檔案路徑加r,不需要對\進行轉義    #檔案路徑中表示斜線    testFile=open(r"E:\PythonDemo\CorePythonBook2\output","r")    for line in testFile.readlines():      #通過格式化字串      #優先使用repr()函數轉換為原始字元      line="%r" %line      #對匹配模式進行加r處理      #匹配的字元轉換為原始字元      m=re.search(r"\\[rn]",line)      #字串中是否包含匹配字元      #如果包含輸出該字串      if m is not None:        print line  except Exception,e:    print e  finally:    testFile.close()#Unicode操作符函數def  unicodeOper():  print u"abc"  print u"\u1234"  print u"abc\u1234\n"  print ur"Hello\nWorld!"#調用函數#輸出操作原始字元結果originalOper()#輸出Unicode字串unicodeOper()

運行結果如下:

PS:這裡再為大家提供幾款Unicode編碼轉換操作相關工具供大家參考使用:

線上Unicode/中文轉換工具:
http://tools.jb51.net/transcoding/unicode_chinese

Native/Unicode線上編碼轉換工具:
http://tools.jb51.net/transcoding/native2unicode

線上中文漢字/ASCII碼/Unicode編碼互相轉換工具:
http://tools.jb51.net/transcoding/chinese2unicode

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.