Re: [問題] 如何刪除特定符號

看板Python作者 (baker)時間14年前 (2010/12/07 19:08), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《david8811 (Naruto)》之銘言: : def num(): : fin = open ("testdata.txt") : for line in fin: : word = line.strip() : word = line.split(' ') : import re : pun_replace = re.compile("[!.'?,]+") : word = pun_replace.sub("",line) : q = ''.join(word) : print q : num() : 如果我現在要把全部的字都黏在一起...像pig head 變成pighead,為什麼我用 : ''.join(word)沒辦法把字年在一起呢? 我在網路上找到可以用空白字串把它們黏在一起 1.把impore re和pun_replace = re.compile("[!.?]+")寫在副程式外面... 因為這二行不用每次都執行 2.在副程式中宣告global pun_replace即可。 3.python的shell可以查詢到程式的說明,例如輸入 help(''.join) 可以找到答案如下: S.join(sequence) -> string join裡面是字串型態的序列,例 ['python',"cat"..]..等。 使用q=''.join(word.split())即可 請問這些東西是作業麻? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.166.94

12/07 21:20, , 1F
課後延伸練習,下課前解答...用不同的方式會加分@@"
12/07 21:20, 1F
文章代碼(AID): #1C_XMORZ (Python)
討論串 (同標題文章)
文章代碼(AID): #1C_XMORZ (Python)