0

I have an output file which created from a bash script.

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

 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?

David Jones
  • 4,766
  • 3
  • 32
  • 45
1010111100011
  • 83
  • 1
  • 3
  • 9
  • 1
    using [xlsxwriter](https://xlsxwriter.readthedocs.io) ? – oguz ismail Sep 06 '18 at 10:27
  • 1
    Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself. – Cyrus Sep 06 '18 at 11:00

1 Answers1

1

You can do that pretty very elegantly with pandas Dataframes. Check pandas.read_csv and pandas.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 (e.g. here) and try those, before jumping on pandas.

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.

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41