看板 Python 關於我們 聯絡資訊
小弟是接觸Python一週的新手, 讀了《深入淺出程式設計》後,想寫個程式看看。 Google和爬文後 寫了以下程式碼: #!/usr/bin/python # -*- coding: big5 -*- import telnetlib tn = telnetlib.Telnet('ptt.cc') tn.expect(['請輸入代號']) tn.write('asdfjkl\r\n') 但卻顯示: Traceback (most recent call last): File "C:/Users/Peanut/PythonProject/LoginPTT/loginPTT.py", line 5, in <module> tn.expect(['請輸入代號']) File "C:\Python33\lib\telnetlib.py", line 652, in expect return self._expect_with_select(list, timeout) File "C:\Python33\lib\telnetlib.py", line 735, in _expect_with_select m = list[i].search(self.cookedq) TypeError: can't use a string pattern on a bytes-like object 把'請輸入代號'前面加b,變成:tn.expect([b'請輸入代號']) 按F5執行,就跳出bytes can only contains ASCII literal characters. 請問各位前輩, 1.程式開頭指定用big5 是什麼意思? 在這種bbs的環境下是適合的嗎? 2.在b'字串' ,這個代表什麼函意呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.24.92.67
ck574b027:1 跟 bbs 沒有關係,它是標示這個 py 檔用的編碼。 11/19 00:31
icycandle:2 可google python String literals 11/19 19:07