Python Forum

Full Version: [split] Results of this program in an excel file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi.
I want to get the results of this program in an excel file.
Nine numbers.I have the results in terminal in python without any problem but how can I get
these nine numbers in an excel file separately ?



from fileinput import filename
from turtle import shape
import numpy as np
import pandas as pd
import xlsxwriter
from xlsxwriter.utility import xl_rowcol_to_cell
from openpyxl import workbook
from openpyxl.utils import get_column_letter
from datetime import datetime
import xlwt 
from xlwt import Workbook


for  i  in range(101000,101009) :
    
     j=str(i)
     
     first_digit=int(j[0])
     second_digit=int(j[1])
     third_digit=int(j[2])
     fourth_digit=int(j[3])
     fifth_digit=int(j[4])
     sixth_digit=int(j[5])
     
     if second_digit*2 > 9 :
        secondigit=second_digit*2 - 9  
     else   :
        secondigit= second_digit*2

     if  fourth_digit*2 > 9 :
         fourthdigit=fourth_digit*2 - 9  
     else   :
         fourthdigit= fourth_digit*2

     if  sixth_digit*2 > 9 :
         sixthdigit=sixth_digit*2 - 9  
     else   :
         sixthdigit= sixth_digit*2

     numbersum=(first_digit)+(secondigit)+(third_digit)+(fourthdigit)+(fifth_digit)+(sixthdigit)
     # print(numbersum)             

     check_digit=((10-(numbersum%10))%10)
                 
     def add_digit_right(i,check_digit) :
         return str(i)+str(check_digit)
      
     df=add_digit_right(i,check_digit)
     print(df)
     
You most first make into a data structure eg dictionary.
Add before loop and last.
record = {}
...
record.setdefault('Numbers', []).append(df)
Test and then first into then into a DataFrame and use .to_excel().
>>> import pandas as pd
>>> 
>>> record
{'Numbers': ['1010008',
             '1010016',
             '1010024',
             '1010032',
             '1010040',
             '1010057',
             '1010065',
             '1010073',
             '1010081']}

>>> df = pd.DataFrame(record)
>>> df
   Numbers
0  1010008
1  1010016
2  1010024
3  1010032
4  1010040
5  1010057
6  1010065
7  1010073
8  1010081
>>> df.to_excel('output.xlsx', index=False)