python錯誤總結

來源:互聯網
上載者:User

標籤:能力   找不到   user   des   keyword   ror   write   last   成長   

  • csv檔案寫入中文亂碼問題以及讀寫錯誤

    在python2.7中,運行程式以後出現以下錯誤。

1 Traceback (most recent call last):2 13   File "C:/Users/Administrator/Desktop/python_code/untitled0.py", line 57, in <module>4     file=open(‘da.csv‘, ‘w‘,newline=‘‘)5 TypeError: ‘newline‘ is an invalid keyword argument for this function

  通過度娘搜尋後才知道,這是由於python3.x版本中open()函數的用法不一樣。

1 # 把 file=open(‘da.csv‘, ‘w‘,newline=‘‘)改成下面這個即可解決2 file=open(‘da,csv‘,‘wb‘) # python2.x中使用“wb”就能避免出現空行3 file.write(codecs.BOM_UTF8) # 解決中文亂碼問題

   解決這個問題需要再開頭加上下面這行代碼:

1 import sys2 import codecs3 reload(sys)4 sys.setdefaultencoding(‘utf-8‘)

 

   總結:

    雖然是個小小的錯誤,但是沒有搞清楚之前還是想了半天,找不到錯誤的原因。

    編程能力還是有待提高,得多動手練習,才能在一個一個的錯誤中逐漸成長起來。

    排解焦慮的最好方法,就是立即行動。

python錯誤總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.