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

2024-09-25 08:27:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从bash脚本创建的输出文件。你知道吗

输出文件是:

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

我正试图创建Excel文件,从这将是

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

如何使用Python实现这一点?你知道吗


Tags: 文件脚本bashlocalrootexcelprotocolxxx
1条回答
网友
1楼 · 发布于 2024-09-25 08:27:05

使用熊猫数据帧可以非常优雅地实现这一点。检查pandas.read_csvpandas.DataFrame.to_excel。你知道吗

如果您是Python新手,并且到目前为止还没有与pandas合作过,我建议您在stackoverflow中查找Python/xls问题(例如here),并在开始讨论pandas之前尝试这些问题。你知道吗

无论如何,如果您需要快速解决方案,请复制并粘贴此

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)

你想怎么做就怎么做。你知道吗

相关问题 更多 >