python 常用api

1. python 2/3 區別

  • 整除
python 2:
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0
結果:
    3 / 2 = 1
    3 // 2 = 1
    3 / 2.0 = 1.5
    3 // 2.0 = 1.0
python3:
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)
    3 / 2 = 1.5
    3 // 2 = 1
    3 / 2.0 = 1.5
    3 // 2.0 = 1.0
  • Unicode
    Python 2 有 ASCII str() 類型,unicode() 是單獨的,不是 byte 類型。
    現在, 在 Python 3,我們最終有了 Unicode (utf-8) 字符串,以及一個字節類:byte 和 bytearrays。
    由於 Python3.X 源碼文件默認使用utf-8編碼,這就使得以下代碼是合法的:

2. json api

  • 從json str 轉成json
for line in f:
	data = json.loads(line)
	one_data = data.values()
  • 按照文本順序
from collections import OrderedDict

for line in f:
	data = json.loads(line, object_pairs_hook=OrderedDict)
	one_data = data.values()
  • json dump 輸出文本
json.dumps(arr, ensure_ascii=False).encode('utf8')

3. 文件讀寫

4. 多進程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章