標籤:模組 驅動 table 寫入檔案 min 資料處理 linux session filename
CSV資料處理
csv檔案格式
逗號分隔字元(csv),有時也稱為字元分隔值,因為分隔字元也可以不是逗號,其檔案以純文字的形式儲存表格式資料(數字和文本)。
純文字意味著該檔案是一個字元序列,不含必須像位元字那樣被解讀的資料。
csv檔案由任意數目的記錄組成,記錄間以某種分行符號分割;每條記錄由欄位組成,欄位間的分隔字元是其他字元或字串,最常見的是逗號或定位字元。通常,所有記錄都有完全相同的欄位序列。
csv資料格式
27,20,14,15,14,12,94,64,37,1015,1013,1009,7,5,2,21,8,35,0.00,152
另外,csv檔案可以直接用excel或者類似軟體開啟,樣子都是我們常見的表格形式。
常用讀取資料方法
import codecslineText = list()with codecs.open("test.csv",encoding="utf-8") as f: for line in f.readlines(): print (line.split(",")) #以列表形式,列印每一行的資料。 lineText.append(line.split(",")) print (lineText) #把上面所有行作為元素資料,存入一個列表中。
處理csv格式資料
import codecsimport csvfileName = "test.csv"with codecs.open(fileName) as fcsv: linecsv = csv.reader(fcsv) rows = [row for row in linecsv] print (rows)
excel資料處理
python提供有第三方庫來支援excel的操作,python處理excel檔案用的第三方模組庫,有xlrd、xlwt、xluntils和pyExcelerator,除此之外,python處理excel還可以用win32com和openpyxl模組.我們主要用xlrd、xlwt、xluntils這三個模組,pyExcelerator模組偶爾也會用。
xlrd 只能進行讀取excel檔案,沒法進行寫入檔案;xlwt 可以寫入檔案,但是不能在已有的excel的檔案上進行修改;xluntils 可以在已有的excel檔案上進行修改;pyExcelerator 與xlwt類似,也可以用來產生excel檔案
相關模組安裝
pip install xlrdpip install xlwtpip install xlutilspip install pyExcelerator
按行讀取表資料
import xlrddef readExcel(): data = xlrd.open_workbook(‘test.xlsx‘) table = data.sheets()[0] # 開啟第一張表 nrows = table.nrows # 擷取表的行數 for i in range(nrows): # 迴圈逐行列印 print(table.row_values(i)) #通過row_values來擷取每行的值 if __name__ == ‘__main__‘: readExcel()
按列讀取表資料
import xlrddata = xlrd.open_workbook("whsc.xlsx")table2 = data.sheet_by_name("網域名稱") #sheet標籤頁的名稱for col in range(table2.ncols): print (table2.col_values(col))
建立excel檔案並寫入內容
import xlwtexcel = xlwt.Workbook()#建立3個表sheet1 = excel.add_sheet("sheet1")sheet2 = excel.add_sheet("sheet2")sheet3 = excel.add_sheet("sheet3")#只在第一個表sheet1裡寫資料,如下:sheet1.write(0,0,"hello world1", cell_overwrite_ok=True)sheet1.write(1,0,"hello world2", cell_overwrite_ok=True)sheet1.write(2,0,"hello world3", cell_overwrite_ok=True)#第一個是行,第二個是列,第三個是內容,第二個參數用來確認同一個cell單元是否可以重設值。excel.save("hello.xlsx")print("建立hello.xlsx完成")
使用樣式、字型等效果
import xlwtexcel = xlwt.Workbook()#建立3個表sheet1 = excel.add_sheet("sheet1")sheet2 = excel.add_sheet("sheet2")sheet3 = excel.add_sheet("sheet3")#初始化樣式style = xlwt.XFStyle()#為樣式建立字型font = xlwt.Font()font.name = ‘Times New Roman‘ #指定字型名稱font.bold = True #是否加粗#設定樣式的字型style.font = font#使用樣式sheet3.write(0,1,‘some bold Times text‘,style)#儲存該excel檔案,有同名檔案時直接覆蓋excel.save(‘hello.xlsx‘)print(‘建立hello.xlsx檔案完成!‘)
檔案轉換成pdf格式
在工作中,會遇到把html檔案轉換成pdf檔案,轉換成pdf有三種方法。
python給我們提供了pdfkit這個模組,直接安裝使用就可以了。
安裝該模組
pip install pdfkit
簡單例子
import pdfkitpdfkit.from_file("hello.html", 1.pdf) # 網頁轉換成pdf(直接把url轉換成pdf檔案)pdfkit.from_url("www.baidu.com", 2.pdf) # Html轉換成pdfpdfkit.from_string("hello world", 3.pdf) # 字串轉換成pdf
抓取apelearn上的教程,並抓換成pdf
import osimport reimport pdfkitimport requests if not os.path.exists("aminglinux"): os.mkdir("aminglinux") # 建立一個目錄來存放產生的pdf檔案 os.chdir("aminglinux") # 切換到建立好的目錄 url = "http://www.apelearn.com/study_v2/"s = requests.session()text = s.get(url).textreg = re.compile(r‘<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"(.*)\">.*<\/a><\/li>‘)result = reg.findall(text)res = list(set(result)) for i in res: purl = "{0}{1}".format(url, i) print (purl) pdfFileName = i.replace("html", "pdf") print (pdfFileName) config = pdfkit.configuration(wkhtmltopdf=r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe") try: pdfkit.from_url(purl, pdfFileName, configuration=config) except: continue
結果:
chapter1.pdfchapter2.pdfchapter3.pdfchapter4.pdfchapter5.pdf............
注意:如果使用的是windows需要安裝一個wkhtmltopdf驅動,否則會報錯。
python處理資料