我是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的文件中?
答案 0 :(得分:0)
我想您可以使用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()