Time of Update: 2016-10-25
解決Python中字串和數字拼接報錯的方法,python拼接前言眾所周知Python不像JS或者PHP這種弱類型語言裡在字串串連時會自動轉換類型,如果直接將字串和數字拼接會直接報錯。如以下的代碼:# coding=utf8str = '你的分數是:'num = 82text = str+num+'分 | 瓊台部落格'print text執行結果直接報錯:TypeError: cannot concatenate 'str' and 'int'
Time of Update: 2016-10-25
python
Time of Update: 2016-10-25
如何在Python函數執行前後增加額外的行為,python函數執行首先來看一個小程式,這個是計量所花費時間的程式,以下是以往的解決樣本from functools import wraps, partialfrom time import timedef timing(func=None, frequencies=1): if func is None: # print("+None") return partial(timing, frequencies=frequencies) #
Time of Update: 2016-10-25
Python做文本按行去重的實現方法,文本:每行在promotion後麵包含一些數字,如果這些數字是相同的,則認為是相同的行,對於相同的行,只保留一行。思路:根據字典和字串切割。建立一個空字典。讀入文本,並對每行切割前半部分,在讀入文本的過程中迴圈在這個字典中尋找,如果沒找到,則寫入該行到字典。否則,則表示該行已經被寫入過字典了(即出現重複的行了),不再寫入字典,這就實現了對於重複的行只保留一行的目的。文本如下:/promotion/232 utm_source/promotion/237
Time of Update: 2016-10-25
Python類屬性的延遲計算,python屬性延遲所謂類屬性的延遲計算就是將類的屬性定義成一個property,只在訪問的時候才會計算,而且一旦被訪問後,結果將會被緩衝起來,不用每次都計算。優點構造一個延遲計算屬性的主要目的是為了提升效能實現class LazyProperty(object): def __init__(self, func): self.func = func def __get__(self, instance, owner): if instance
Time of Update: 2016-10-25
Python檢測生僻字的實現方法,python檢測生僻字解決思路首先想到的就是利用 python 的Regex來匹配非法字元,然後找出非法記錄。然而理想總是豐滿的,現實卻是殘酷的。在實現的過程中,才發現自己對於字元編碼、以及 python 內部字串表示的相關知識的缺乏。在這期間,踩過了不少坑,到最後雖然還有些模糊的地方,但總算有一個總體清晰的瞭解。在此記錄下心得,避免以後在同一個地方跌倒。以下的測試環境是 ArcGIS 10.3 內建的 python 2.7.8 環境,不保證其他 python
Time of Update: 2016-10-25
windows下ipython的安裝與使用詳解,ipython使用詳解ipython的安裝ipython可以直接使用pip install ipython安裝 ,如果安裝失敗按如下步驟手動進行安裝所需檔案下載: 連結:http://pan.baidu.com/s/1dE756OL 密碼:rlzhipython是一個 python 的互動式 shell,比預設的python shell 好用得多,支援變數自動補全,自動縮排,支援 bash shell
Time of Update: 2016-10-24
格式化 float 類型,保留小數點後1位,float小數點 1 """ 練習 : 2 小明的成績從去年的72分提升到了今年的85分,請計算小明成績提升的百分點, 3 並用字串格式化顯示出'xx.x%',只保留小數點後1位: 4 """ 5 # NOTE: 格式化 float 類型保留位元輸出 : 6 # ( '%0.1f' %r) --(保留小數點後1位) 7 # ( '%0.2f' %r)
Time of Update: 2016-10-25
Python 內建函數complex詳解,pythoncomplex英文文檔:class complex([real[, imag]])Return a complex number with the value real + imag*1j or convert a string or number to a complex number. If the first parameter is a string, it will be interpreted as a complex
Time of Update: 2016-10-25
如何利用Fabric自動化你的任務,fabric自動化任務首先讓我們首先看一個例子。我們知道在*NIX下面,uname命令是查看系統的發行版。可以寫這樣一個Fabric指令碼:from fabric.api import rundef host_type(): run('uname -s')將上面的指令碼儲存為fabfile.py,就可以通過fab命令在多個主機上執行host_type指令碼了:$ fab -H localhost,linuxbox host_type[localhost]
Time of Update: 2016-10-24
Python內建函數(15)——dict,python內建函數dict英文文檔:class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.If no
Time of Update: 2016-10-25
python 讀寫txt檔案 json檔案的實現方法,首先第一步,開啟檔案,有兩個函數可供選擇:open() 和 file()①. f = open('file.txt',‘w') ... file.close() ②. f = file('file.json','r') ...file.close()#記得開啟檔案時最後不要忘記關閉!open() 和 file()
Time of Update: 2016-10-25
簡單談談Python中的反轉字串問題,python字串按單詞反轉字串是一道很常見的面試題。在Python中實現起來非常簡單。def reverse_string_by_word(s): lst = s.split() # split by blank space by default return ' '.join(lst[::-1])s = 'Power of Love'print reverse_string_by_word(s)# Love of Powers = 'Hello
Time of Update: 2016-10-24
【leetcodepython】 257. Binary Tree Paths,leetcodepython深度優先搜尋# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val =
Time of Update: 2016-10-24
python 配置開發環境 安裝mysql-connector-python,pythonmysqlconnectorDay1:今天開始跟著廖雪峰老師做一個基於python的部落格系統,第一天配置python開發環境;參考網站: 實戰;本機環境: MacOS 10.12問題: 安裝mysql-connector-python中遇到的問題網上推薦的安裝方法: $ easy_install mysql-connector-python, 但是在安裝過程中出現了以下問題:[Errno 13]
Time of Update: 2016-10-24
自學python核心編程,自學python核心 之前看過一段時間的小甲魚零基礎自學python,b站上有高清免費資源[av4050443],但是作為零基礎實在學得艱難,下載了python核心編程pdf,在這裡做一些筆記。 雖然使用的是第二版的教材,但我使用的是python3.5.2,感覺上差別不大,可以閱讀。 這裡從第二章的練習題開始! ------------------------------------------------------
Time of Update: 2016-10-24
Python安裝mysql-python錯誤提示python setup.py egg_info,setup.pyegginfo做python項目,需要用到mysql,一般用python-mysql,安裝時遇到錯誤提示如下:Command "python setup.py egg_info" failed with error code 1Trace的關鍵資訊是:sh: mysql_config: command not
Time of Update: 2016-10-23
python列表的使用之深淺copy,python深淺copyimport copy #引用模組copy #深,淺copynames=['Lishan','Yuanming',['Quyan','Liuzhen','Pengqiang'],'Fanyi','Jixue']name2 = names.copy() #淺copy 三種方式name3 = copy.copy(names) #淺copyn2 = names[:] #淺copyn3 =
Time of Update: 2016-10-24
python爬蟲成長之路(二):抓取代理IP並多線程驗證,python成長之路 上回說到,突破反爬蟲限制的方法之一就是多用幾個代理IP,但前提是我們得擁有有效代理IP,下面我們來介紹抓取代理IP並多線程快速驗證其有效性的過程。 一、抓取代理IP
Time of Update: 2016-10-23
Python內建函數(13)——complex,pythoncomplex英文文檔:class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert a string or number to a complex number. If the first parameter is a string, it will be interpreted as a complex