推 tiefblau: 直接print到檔案 如果是py2 print >> f, i 04/26 20:17
→ tiefblau: py3: print(i, file=f) 或是執行的時候直接重導向 04/26 20:18
→ busystudent: 可以再說明清楚一點嗎?我是用py2 04/26 20:19
→ busystudent: print >>f,i是指什麼? 04/26 20:20
→ MOONY135: a=i是多餘的吧 04/26 21:52
※ 編輯: busystudent (1.172.95.247), 04/26/2016 23:02:45
※ 編輯: busystudent (1.172.95.247), 04/26/2016 23:07:06
※ 編輯: busystudent (1.172.95.247), 04/26/2016 23:12:41
→ checkIO: str(d) 04/26 23:28
→ checkIO: 基本上error資訊丟去Google就有解答的事情了 04/26 23:31
推 aweimeow: 第一個問題:改用codecs.open('abc.txt', 'w', 'utf-8') 04/26 23:31
→ aweimeow: 記得要 import codecs 哦 04/26 23:31
→ aweimeow: 然後第二個問題就如同上面 c大 所說 04/26 23:32
→ aweimeow: 他錯誤訊息就寫著 float 不能 和 str 結合 04/26 23:32
→ aweimeow: 所以你可以把 float 轉成 string 或是 "%f\n" % d 04/26 23:32
→ busystudent: 感謝留言,所以第一個問題可以推測是txt不支援utf-8` 04/26 23:35
→ busystudent: ,要多宣告嗎? 04/26 23:35
→ checkIO: 編碼問題 抓下來可能是無法辨識的 04/26 23:37
→ checkIO: 因為你一開始宣告是utf8 不然把utf8那行拿掉試試看 04/26 23:39
→ busystudent: 等等 我忽然想到那我要開始就宣告萬國碼 04/26 23:41
→ busystudent: 問題會解決嗎? 04/26 23:41
推 aweimeow: 不會吧我覺得 04/26 23:48
→ octantis: 你們是不是誤會了什麼,開頭宣告的utf8只是表示這份文件 04/27 00:09
→ octantis: 要用哪種編碼來將py檔編成二進位檔,跟你的open一點關係 04/27 00:10
→ octantis: 也沒有,你的open並write預設用的編碼是看sys模組裡的 04/27 00:11
→ octantis: sys.getdefaultencoding(),它才是判斷你的資料該以何種 04/27 00:13
→ octantis: 編碼編成二進位並寫入文件。跟你用beautifulsoup抓進來 04/27 00:14
→ octantis: 的資料也一點關係也沒有,BeautifulSoup會把抓到的資料 04/27 00:16
→ octantis: 轉成Unicode不管資料原本是何種編碼,讀進來就是Unicode 04/27 00:16
→ octantis: 此外開頭的宣告utf8只是'告訴'別人你這份py檔要用什麼編 04/27 00:21
→ busystudent: 了解,感謝說明 04/27 00:21
→ octantis: 碼,並不代表你實際的編碼,你也可以開頭宣告utf8,但把 04/27 00:21
→ octantis: py檔存成ascii,因此在存檔時需特別注意你的存檔編碼跟 04/27 00:23
→ octantis: py檔開頭的宣告是不是同一個,否則別人看你開頭是utf8但 04/27 00:24
→ octantis: 你存成其他的就有可能出現py檔亂碼等情形 04/27 00:25
推 krazy1101: 最近也遇到這問題 推 04/27 14:12