python 讀、取、寫入excel表,注意list和dict


運用python 讀進excel表、取字段、寫入excel表

 

########################################
#讀取excel,取某一行列值,將某行列值保留成excel形式
#######################################

 

from pandas import DataFrame
import xlrd

import os
os.chdir(r"C:\Users\excel")

worksheet = xlrd.open_workbook(r'excel.xlsx')
sheet_names= worksheet.sheet_names()
sheet_name=sheet_names[0]##選擇哪個表
sheet2 = worksheet.sheet_by_name(sheet_name)
rows = sheet2.row_values(3) # 獲取第四行內容
cols = sheet2.col_values(1) # 獲取第二列內容
print (rows)
print (cols)
cols.pop(0)####這個是去掉第一行的意思

#我不喜歡用這個for循環,但這個循環也是另外一種方法
#for sheet_name in sheet_names:
# sheet2 = worksheet.sheet_by_name(sheet_name)
print (sheet_name)
rows = sheet2.row_values(3) # 獲取第四行內容
cols = sheet2.col_values(1) # 獲取第二列內容
print (rows)
print (cols)

 

 

##寫入excel表里
url = sheet2.col_values(1) # 獲取第二列內容
zone = sheet2.col_values(2) # 獲取第二列內容
data = {'zone':zone,'url':url}
#del data["url"] #這個是刪除了整一列
frame=DataFrame(data,columns=['zone','url'])
frame.to_excel(r'writexcel.xlsx',index=False)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM