【问题标题】:Print and save output to excel打印并保存输出到excel
【发布时间】:2017-03-12 04:29:02
【问题描述】:

我是学习python的新手,我有将输出打印到python控制台的代码,现在我想将输出打印到控制台并将其保存在excel文件中

  def print_results(results):



print()
  print('Profile Name: %s' % results.get('profileInfo').get('profileName'))
  print()

  # Print header.
  output = []
  for header in results.get('columnHeaders'):
    output.append('%30s' % header.get('name'))
  print(''.join(output))

  # Print data table.
  if results.get('rows', []):
    for row in results.get('rows'):
      output = []
      for cell in row:
        output.append('%30s' % cell)
      print(''.join(output))

  else:
    print('No Rows Found')

【问题讨论】:

标签: python excel python-2.7


【解决方案1】:

如果您有 pandas,您可以将输出保存在 pandas DataFrame 中,然后使用

将其保存到 excel 文件中
DataFrame.to_excel("Output_File.xlsx")

【讨论】:

    【解决方案2】:

    模块 xlwt 可以做到 f = xlwt.Workbook() f.save('demo1.xlsx')

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 2018-03-09
      • 2019-04-08
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多