Hello, Python!

搞了一下午的Python,從找KNIME的SNA組件,到可視化social graph,再到iGraph和NetworkX,最後再到Python……

參考了閆小勇老師的博文和總結《從C#到Python》,百度一下到處都是,這裏就不給鏈接了。41頁,打印出來,很實用,很入門。
裝了Python2.7,雖說現在Python已經有了3.x版本,但iGraph等工具包還不支持。
裝了之後,又找IDE,pythonwin、PyQT配Eric4統統失敗,最後鎖定了PyScripter,終於跑出了第一個Hello World。
用PyScripter時,需要修改一下Python的Lib目錄下的site.py,否則永遠有形如“UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)”的錯誤。site.py中有個函數如下:
def setencoding():
    """Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this."""
    encoding = "ascii" # Default value set by _PyUnicode_Init()
    if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        # Enable to switch off string to Unicode coercion and implicit
        # Unicode to string conversion.
        encoding = "undefined"
    if encoding != "ascii":
        # On Non-Unicode builds this will raise an AttributeError...
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !
很奇怪爲什麼是兩個if 0,把第一個0改成1就不會有上面那個錯誤了。不知是不是python的bug。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章