Python Tips & Tricks

读写

  • for line in opened_file: 逐行读取,读一行加载一行
  • for line in opened_file.readlines(): 逐行读取,将所有内容加载到内存
  • for row in cursor: 逐row读取,读一行加载一行
  • for row in cursor.fetchall() : 逐row读取,将所有内容加载到内存

注意: 单次循环中,计算任务所花的时间 >> 单次IO读取的时间时, for line in file/cursor 更好 单次循环中,计算任务所花的时间 << 单次IO读取的时间时, for line in file.readlines()/cursor.fetchall() 更好


In [16]:
%matplotlib inline

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

rs = np.random.RandomState(10)
# Plot a historgram and kernel density estimate
sns.distplot(rs.normal(size=100), color="m")


/Users/marc/anaconda/lib/python3.5/site-packages/statsmodels/nonparametric/kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x11be6d860>

In [ ]: