Python Forum
[split] Results of this program in an excel file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Results of this program in an excel file
#1
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)
     
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 344 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,265 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 856 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  How to "tee" (=split) output to screen and into file? pstein 6 1,409 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,084 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 824 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Split pdf in pypdf based upon file regex standenman 1 2,101 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  Import XML file directly into Excel spreadsheet demdej 0 861 Jan-24-2023, 02:48 PM
Last Post: demdej

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020