【问题标题】:Add values to the file from excel sheet with pyhon使用python从excel表中向文件添加值
【发布时间】:2019-04-28 01:56:55
【问题描述】:

我是 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 的文件中?

【问题讨论】:

    标签: python xlrd


    【解决方案1】:

    我想你可以使用 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()
    

    【讨论】:

    • 感谢您的宝贵时间,但我希望在文本文件中删除 HOST_NAME 行中的 HOST_NAME 列:类似于 `HOST_NAME host1.example.com host2.example.com host3.example。 com`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多