天天看點

Python資料寫入csv格式檔案

Python資料寫入csv格式檔案

(隻是傳遞,基礎知識也是根基)

Python讀取資料,并存入Excel打開的CSV格式檔案内!

這裡需要用到bs4,csv,codecs,os子產品。

廢話不多說,直接寫代碼!該重要的内容都已經注釋了,剩下不懂的可以自己查詢一下,或者QQ群内問我。QQ群在以往的部落格中!

1 #coding:utf-8 
 2 from bs4 import BeautifulSoup
 3 import bs4
 4 import os
 5 import time
 6 import csv
 7 import codecs
 8 
 9 #讀取XML内的檔案資料并存入CSV格式的檔案--可使用EXCEL打開
10 def open_file():
11     file_folder= 'C:\\Users\\Administrator\\Desktop\\File\\Filename' ##檔案夾位置
12     if os.path.isdir(file_folder):
13         for fileName in os.listdir(file_folder):
14            # print fileName
15             info(fileName) ##讀取檔案名字
16 def info(fileName):
17     soup = bs4.BeautifulSoup(open('C:/Users/Administrator/Desktop/File/Filename/'+fileName))
18     a = soup.find_all('mxxx')
19     info = []
20     for i in a:
21         dt=[]
22         dt.append(i.find('xx').get_text().strip())
23         dt.append( i.find('xx').get_text().strip())
24         dt.append(i.find('xx').get_text().strip())
25         dt.append(i.find('xx').get_text().strip()+'\n')
26         dt.append( i.find('xx').get_text().strip())
27         dt.append(i.find('xx').get_text().strip())
28         dt.append(float( i.find('xx').get_text().strip())  + float(i.find('xx').get_text().strip()))
29         info.append(dt)
30     with open("Ex_info.csv","ab+") as csvfile: ##“ ab+ ”去除空白行,又叫換行!
31         csvfile.write(codecs.BOM_UTF8)  ##存入表内的文字格式
32         writer = csv.writer(csvfile)  #存入表時所使用的格式
33         writer.writerow(['表頭','表頭'])
34         writer.writerows(info) #寫入表
35     
36 if __name__ == '__main__':
37     open_file()      

這裡主要三部分,我調用的測試使用的xml内的資料,這裡使用的bs4來解析xml檔案。

解釋第一部分:

打開檔案夾,并擷取到檔案的名字,因為檔案有多個,是以使用了os子產品的函數來打開檔案

1  file_folder= 'C:\\Users\\Administrator\\Desktop\\File\\Filename' ##檔案夾位置
2     if os.path.isdir(file_folder):
3         for fileName in os.listdir(file_folder):
4            # print fileName
5             info(fileName) ##讀取檔案名字      

fileName就是我們所擷取的檔案的名字。

第二部分:

擷取檔案名字後需要使用bs4子產品來打開檔案,因為多個檔案,是以将解析過程寫入函數内。

1 def info(fileName):
 2     soup = bs4.BeautifulSoup(open('C:/Users/Administrator/Desktop/File/Filename/'+fileName))
 3     a = soup.find_all('mxxx')
 4     info = []
 5     for i in a:
 6         dt=[]
 7         dt.append(i.find('xx').get_text().strip())
 8         dt.append( i.find('xx').get_text().strip())
 9         dt.append(i.find('xx').get_text().strip())
10         dt.append(i.find('xx').get_text().strip()+'\n')
11         dt.append( i.find('xx').get_text().strip())
12         dt.append(i.find('xx').get_text().strip())
13         dt.append(float( i.find('xx').get_text().strip())  + float(i.find('xx').get_text().strip()))
14         info.append(dt)      

打開檔案夾後取出檔案,并解析後使用BeautifulSoup的解析網頁方法來擷取資料,'mxxx'與'xx'都是樹的名字。

第三部分:

将資料寫入csv檔案,這裡的資料都是List格式,并需要周遊。

1 with open("Ex_info.csv","ab+") as csvfile: ##“ ab+ ”去除空白行,又叫換行!
2         csvfile.write(codecs.BOM_UTF8)  ##存入表内的文字格式
3         writer = csv.writer(csvfile)  #存入表時所使用的格式
4         writer.writerow(['表頭','表頭','表頭','表頭'])
5         writer.writerows(info) #寫入表      

這裡的表頭要和我們上面擷取的資料列一緻,不然會出現錯誤。寫入檔案的格式有 “ w ”," a+ "," ab+ "等,這裡使用的是“ ab+ ”,去除空行!

還有我們如果存入的資料是1000000000這樣的,在excel内是E+17的顯示,所有在擷取資料的時候最後面加  “\n”。最後寫入表内,并打開!

Welcome to Python world! I have a contract in this world! How about you?