簡體   English   中英

使用python從Bash腳本輸出創建Excel文件

[英]Creating Excel File From Bash Script output with 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執行此操作?

您可以使用pandas Dataframe非常優雅地做到這一點。 檢查pandas.read_csvpandas.DataFrame.to_excel

如果您是Python的新手,並且到目前為止還沒有使用過熊貓,我建議您在跳到熊貓上之前先在stackoverflow中查找Python / xls問題(例如here ),然后嘗試這些問題。

無論如何,如果您需要快速解決方案,請復制並粘貼此內容

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)

這將做你想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM