简体   繁体   English

使用python从Bash脚本输出创建Excel文件

[英]Creating Excel File From Bash Script output with python

I have an output file which created from a bash script. 我有一个从bash脚本创建的输出文件。

that output file is: 该输出文件是:

   xyz|xxx.local|Protocol 2|Root Acces Denied
   xyz|xxx1.local|Protocol 2|Root Acces Denied

I am trying to create Excel file from this which is going to be 我正在尝试从中创建Excel文件

 Column1  Column2     Column3       Column4
  xyz    xxx.local   Protocol 2  Root Acces Denied
  xyz    xxx.local   Protocol 2  Root Acces Denied

How can I do this with Python? 如何使用Python执行此操作?

You can do that pretty very elegantly with pandas Dataframes. 您可以使用pandas Dataframe非常优雅地做到这一点。 Check pandas.read_csv and pandas.DataFrame.to_excel . 检查pandas.read_csvpandas.DataFrame.to_excel

If you are new to Python and you haven't worked with pandas so far, I recommend you to look up Python/xls questions here in stackoverflow (eg here ) and try those, before jumping on pandas. 如果您是Python的新手,并且到目前为止还没有使用过熊猫,我建议您在跳到熊猫上之前先在stackoverflow中查找Python / xls问题(例如here ),然后尝试这些问题。

Anyway, if you need a quick solution copy & paste this 无论如何,如果您需要快速解决方案,请复制并粘贴此内容

import pandas as pd

# Load your bash output file in pandas dataframe
df = pd.read_csv('bash_outfile.out', sep='|', names=['Column1', 'Column2',
                                                     'Column3', 'Column4'])

# Open pandas ExcelWriter and write to *.xls file
with pd.ExcelWriter('my_xls.xls') as writer:
    df.to_excel(writer)

which will do what you want. 这将做你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM