1纲岭、Python2中urllib和urllib2模塊是相關(guān)分開的魏铅,各自執(zhí)行不同的功能勉吻,但是在Python3中urllib和urllib2模塊合并了
2谭梗、# coding=utf-8 linux下編輯器的編碼格式為utf-8忘晤,而python的默認(rèn)編譯格式為gbk,因此需要指定編譯格式為utf-8激捏。
漢字作為變量值设塔,默認(rèn)為GBK編碼,需要指定為unicode远舅,使用 u'漢字' 闰蛔。
字符串在Python內(nèi)部的表示是unicode編碼痕钢,因此,在做編碼轉(zhuǎn)換時(shí)序六,通常需要以unicode作為中間編碼任连,即先將其他編碼的字符串解碼(decode)成unicode,再從unicode編碼(encode)成另一種編碼难咕。
decode的作用是將其他編碼的字符串轉(zhuǎn)換成unicode編碼课梳,如str1.decode('gb2312'),表示將gb2312編碼的字符串str1轉(zhuǎn)換成unicode編碼余佃。
encode的作用是將unicode編碼轉(zhuǎn)換成其他編碼的字符串暮刃,如str2.encode('gb2312'),表示將unicode編碼的字符串str2轉(zhuǎn)換成gb2312編碼爆土。
csv文件存儲(chǔ)格式為ascill格式椭懊,因此linux下保存cvs格式還會(huì)報(bào)錯(cuò)。一種方式是將csv人為轉(zhuǎn)換為utf-8格式步势,在csv文件頭部寫入utf-8編碼:
import codecs
csvfile.write(codecs.BOM_UTF8)
另一種方式是在編譯器頭部添加:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
3氧猬、寫入csv
import csv
csvfile=file('csvtest.csv','wb')
writer=csv.writer(csvfile)
writer.writerow(['id','url','keywords'])
writer.wrtierows(list)
csvfile.close()
4、Python可以定義類
class boy:
gender='male'
interest='girl'
def say(self):
return 'i am a boy'
用法和java完全一致坏瘩。
5盅抚、easy_install 和 pip的介紹:
easy_install和pip都是用來下載安裝Python一個(gè)公共資源庫PyPI
的相關(guān)資源包的,pip是easy_install的改進(jìn)版倔矾,提供更好的提示信
息妄均,刪除package等功能。老版本的python中只有easy_install哪自,
沒有pip丰包。
easy_install 打包和發(fā)布 Python 包
pip 是包管理
easy_install的用法:
安裝一個(gè)包
easy_install 包名
easy_install "包名 == 包的版本號"
升級一個(gè)包
easy_install -U "包名 >= 包的版本號"
pip 的用法
安裝一個(gè)包
pip install 包名
pip install 包名 == 包的版本號
升級一個(gè)包(如果不提供version號,升級到最新版本)
pip install --upgrade 包名 >= 包的版本號
刪除一個(gè)包
pip uninstall? 包名
6壤巷、寫入csv文件
import csv
csvfile = file('csv_test.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['姓名', '年齡', '電話'])
data = [
('小河', '25', '1234567'),
('小芳', '18', '789456')
]
writer.writerows(data)
csvfile.close()
7邑彪、將excel中的日期轉(zhuǎn)換為python中的日期
xlrd.xldate.xldate_as_datetime(data[0][0].value,0)