【问题标题】:Python3 write to excel with loopsPython3用循环写入excel
【发布时间】:2022-10-05 11:51:27
【问题描述】:

我花了整整一周的时间尝试以以下格式写入 excel:

Name  Model  Serial  Version
name1 model1 serial1 version1
name2 model2 serial2 version2
name3 model3 serial3 version3

我试过列表、循环、枚举、压缩。我知道这是我正在做的一个愚蠢的逻辑问题,但我没有人向我指出。

无论我对代码做了多少修改,我最终都会在行或列上重写输出,或者编写重复项。

我正在对设备列表运行 API 调用,然后遍历结果以找到有趣的标签,然后将这些标签的文本打印到 Excel 中。

【问题讨论】:

标签: python excel loops


【解决方案1】:

使用熊猫:

import pandas as pd

lines = 3

df = pd.DataFrame([[f'name{i}', f'model{i}', f'serial{i}', f'version{i}'] for i in range(1, lines + 1)],
                  columns=['Name', 'Model', 'Serial', 'Version'])

df.to_excel("myfile.xlsx", index=None)

【讨论】:

    【解决方案2】:

    谢谢你。我能够以不同的方式解决它:

    for row,host in enumerate(file):
         <API calls to host>
         <Append only interesting tags to list 'sys_info'>
        for col in range(len(sys_info)):
           sheet1.write(row+1,col,sys_info[col])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2020-05-24
      相关资源
      最近更新 更多