Home > Software engineering >  How to convert output from python for loop into a table?
How to convert output from python for loop into a table?

Time:06-12

I have written a python code that generates output in the following format:

m=10 n=15 p=0.3 T=0.45

m=10 n=25 p=0.5 T=0.5

m=15 n=15 p=0.3 T=0.65

m=15 n=25 p=0.5 T=0.66

m=20 n=15 p=0.3 T=0.15

.......

I have around 1000 such rows. I want to get the output in a table format so that I can directly copy this into rows and columns of an excel file. Kindly help.

CodePudding user response:

The best way to approach this situation in my opinion is to use dataframes. These dataframes can help you make a table with column names and etc. Pandas can help with you that which is one of the most convenient libraries to use. Here is a quick pseudocode I hope it helps.

import pandas as pd 
list=[]
list.append(["Your desired result"])
df=pd.Dataframe(list)
pd.to_excel(df)

CodePudding user response:

If the result is not much more bigger than that, and it is already generated by for loop, couldn't you just copy the printed for loop results to Excel and then split the columns by space?

CodePudding user response:

The easiest is to initialize a list upfront and append each data point to that list:

result = []

for i in range(1, 10):
    # ... do your calculations
    result.append([i, i*i, i*i*i, 1/i, 1/i/i])

Edit:

What I also sometimes did in the past is to write out the table in html-table format:

<table>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>4</td><td>5</td><td>6</td></tr>
</table>

Normally, writing to an Excel file directly (see the answer from Batselot) can be the better solution. The html-trick allows you to paste the data into an open Excel file where you like to have it.

CodePudding user response:

I wrote a simple script in python, that should do your job.

import sys

print("m,n,p,T")
# read till EOF os stdin
for line in sys.stdin:
    splits = line.rstrip().split(" ")
    if len(splits) != 4:
        continue
    vals = [(lambda x: x.split("=")[1])(x) for x in splits]
    if len(vals) != 4:
        continue
    print(f'{vals[0]},{vals[1]},{vals[2]},{vals[3]}')

It will format the output of your program in csv format which you can load in excel or a spreadsheet.

To do this,

  1. store this script in something like script.py
  2. run this in shell
$ python <your program>.py | python script.py > output.csv
  1. The result will be stored in output.csv which looks like
m,n,p,T
10,25,0.5,0.5
15,15,0.3,0.65
15,25,0.5,0.66
20,15,0.3,0.15

This csv can be loaded into excel.

CodePudding user response:

I think as @Batselot told, the best way to do that would be with DataFrames. You can do one of the two following procedures:

1 - concatenate all the output values of your function in a single string, and then to the following code, which will treat the string, removing the '\n' and ' ':

values = """m=10 n=15 p=0.3 T=0.45

m=10 n=25 p=0.5 T=0.5

m=15 n=15 p=0.3 T=0.65

m=15 n=25 p=0.5 T=0.66

m=20 n=15 p=0.3 T=0.15"""

str_result = values.split('\n')
while '' in str_result:
    str_result.remove('')
data = []
for row in str_result:
    data.append(row.split(' '))
df = pd.DataFrame(data, columns=['m', 'n', 'p', 'T'])
#If my excel file were called 'excel_file.xlsx' and it is in the same folder of my python code and it had a sheet called 'Sheet1'.
df.to_excel(r"excel_file.xlsx", sheet_name='Sheet1', index=False)

2 - You can just append the values straight in a list inside your loop, and you wont have to treat it with string manipulation.

data = []
#You Probably have a for loop like this:
for i in 'some list':
    print("m={}".format(m), "m={}".format(n), "m={}".format(p), "m={}".format(T))
    data.append(["m={}".format(m), "n={}".format(n), "p={}".format(p), "T={}".format(T)])

df = pd.DataFrame(data, columns=['m', 'n', 'p', 'T'])
print(df)
#If my excel file were called 'excel_file.xlsx' and it is in the same folder of my python code and it had a sheet called 'Sheet1'.
df.to_excel(r"excel_file.xlsx", sheet_name='Sheet1', index=False)

Note: you’ll have to install openpyxl if you get the following error:

ModuleNotFoundError: No module named ‘openpyxl’

You may then use PIP to install openpyxl as follows:

pip install openpyxl
  • Related