今天開始往後都,用python3來寫腳本
1.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 codecs lineText = list() with codecs.open("sl_original_live.csv",encoding="utf-8") as f: for line in f.readlines(): print (line.split(",")) #以列表形式,打印每一行的數據。 lineText.append(line.split(",")) print (lineText) #把上面所有行作爲元素數據,存入一個列表中。
csv模塊用法,使用模塊來處理數據:
import codecs import csv fileName = "sl_original_live.csv" with codecs.open(fileName) as fcsv: linecsv = csv.reader(fcsv) rows = [row for row in linecsv] print (rows)
以上了解即可。
2.excel數據處理
python 提供有第三方庫來支持excel的操作,python處理excel文件用的第三方模塊庫,有xlrd、xlwt、xluntils和pyExcelerator,
除此之外,python處理excel還可以用win32com和openpyxl模塊.
我們主要用xlrd、xlwt、xluntils這三個模塊,pyExcelerator模塊偶爾也會用:
pip install xlrd pip install xlwt pip install xlutils pip install pyExcelerator
xlrd 只能進行讀取excel文件,沒法進行寫入文件;
xlwt 可以寫入文件,但是不能在已有的excel的文件上進行修改;
xluntils 可以在已有的excel文件上進行修改;
pyExcelerator 與xlwt類似,也可以用來生成excel文件
讀取表單數據:
舉例:按行取數據
import xlrd def 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 xlrd data = xlrd.open_workbook("whsc.xlsx") table2 = data.sheet_by_name("域名") #sheet標籤頁的名稱 for col in range(table2.ncols): print (table2.col_values(col))
舉例:創建新的並寫入excel文件(xlwt無法修改原有文件)
import xlwt excel = 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 xlwt excel = 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文件完成!')
效果如下:
舉例:處理超鏈接
import codecs import xlwt book = xlwt.Workbook() sheet_index = book.add_sheet('index') line=0 for i in range(9): link = 'HYPERLINK("{0}.txt", "{1}_11111")'.format(i, i) #{0}.txt是要鏈接的文件,{1}_11111是鏈接文件的內容 sheet_index.write(i, 0, xlwt.Formula(link)) #第一個行號,第二個列號,第三個是寫入內容link,內容裏面超鏈接 book.save('simple2.xlsx') for i in range(0, 9): file = str(i) + ".txt" with codecs.open(file, 'w') as f: f.write(str(i)*10)
效果如下:
3.HTML文件轉化成PDF文件
轉換成pdf的三種方法:
在工作中,會遇到把html文件轉換成pdf文件
python給我們提供了pdfkit這個模塊,直接安裝使用就可以了
下面就下來介紹一個pdfkit模塊的幾種用法
首先,我們安裝該模塊:
pip install pdfkit
1.網頁轉換成pdf(直接把url轉換成pdf文件)
import pdfkit pdfkit.from_url('http://google.com', 'out1.pdf')
2. Html轉換成pdf
import pdfkit pdfkit.from_file('test.html', 'out2.pdf')
3. 字符串轉換成pdf
import pdfkit pdfkit.from_string('Hello lingxiangxiang!', 'out3.pdf')
三種方法簡單例子:
import pdfkit pdfkit.from_file("hello.html", 1.pdf) pdfkit.from_url("www.baidu.com", 2.pdf) pdfkit.from_string("hello world", 3.pdf)
即可生成pdf文件~
舉例:
抓取aming的linux教程,然後製作成pdf文件
先抓取每個的網頁,然後生成pdf文件
(1)查看網頁源代碼
(2)查看源代碼內容
和url對應規律
http://www.apelearn.com/study_v2/chapter2.html
http://www.apelearn.com/study_v2/chapter5.html
http://www.apelearn.com/study_v2/chapter7.html
... ...
通過 https://regex101.com/ 正則網站,把需要的字段給過濾出來
代碼如下(未封裝,回頭有空封裝一下):
import os import re import pdfkit import requests if not os.path.exists("aminglinux"): os.mkdir("aminglinux") os.chdir("aminglinux") url = "http://www.apelearn.com/study_v2/" s = requests.session() text = s.get(url).text reg = 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
執行結果: