簡體   English   中英

使用pyhon將值從Excel工作表添加到文件

[英]Add values to the file from excel sheet with pyhon

我是python的新手,並嘗試通過從excel文件的第二列添加數據來創建文件

excel工作表如下圖所示,

DC_CODE     APP_NAME    HOST_NAME
APAC        Tomcat      host1.example.com
APAC        Nginx       host1.example.com
APAC        Apache      host1.example.com

我正在使用這段代碼來打印值,

import xlrd 
import os.path

wb = xlrd.open_workbook(os.path.join('/home/akbharat/Documents/OCIC/deploy_fed_ocic','podhosts_test_file.xlsx'))
sheet = wb.sheet_by_index(0) 
sheet.cell_value(0, 0) 
for i in range(sheet.nrows): 
    print(sheet.cell_value(i, 2)) 

如何將輸出打印到標題為HOST_NAME的文件中?

我想您可以為此使用pandas庫,以下代碼可能有效:

import pandas as pd
df = pd.read_excel(os.path.join('/home/akbharat/Documents/OCIC/deploy_fed_ocic','podhosts_test_file.xlsx')
writer = pd.ExcelWriter('PythonExport.xlsx')
df[['DC_CODE', 'APP_NAME']].to_excel(writer,'Sheet5')
writer.save()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM