close
2.1 split :字串原本的 split 只能認一個字元,改用 re.split 就可以依據多個字元來切割。例如: re.split(r'[;,\s]\s*', 'asdf fjdk; afed, fjek,asdf, foo')
2.2 starswith/endswith :只要前面或後面幾個字元符合指定字串就傳回 true。除了可以用在字串上,也可以用在 tuple 上。
2.3 用 shell 裡的萬用字元來判斷:利用 fnmatch 模組裡的 fnmatch, fnmatchcase 就可以辦到。要注意的是 fnmatch 會因為平台不同,而有可能判斷不同,Linux 上對大小寫判斷很嚴格,但 Windows 則否。因此 fnmatch('foo.txt', '*.TXT') 在 Linux 上會傳回 false,但在 Windows 則會傳回 true,這時候就得用 fnmatchcase 。
2.4 介紹 regular expression ,也就是 re 模組了,這部分可以講的實在很多。用 re 就可以用規則來判斷字串是否符合。
2.5 取代:字串一般就是用 replace,也可以用 re 模組來做,re.sub (substitude)


arrow
arrow

    elleryq 發表在 痞客邦 留言(0) 人氣()