Python Forum
how to read txt file, and write into excel with multiply sheet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to read txt file, and write into excel with multiply sheet
#11
(Jan-07-2023, 09:13 AM)jacklee26 Wrote: when i run it occur this error, do you know how to fix this
FutureWarning: save is not part of the public API, usage can give unexpected results and will be removed in a future version
writer.save()
Is not a error just a Warning,if look into it.
pandas.ExcelWriter they have deprecated save() so use close() instead.
Or better as they advice use writer as context manager,then no need to save or close.
with pd.ExcelWriter('out.xlsx', engine='openpyxl') as writer:
    df1.to_excel(writer, 'sheet', index=False)
    df2.to_excel(writer, 'sheet1', index=False)
jacklee26 likes this post
Reply
#12
(Jan-07-2023, 01:20 PM)snippsat Wrote:
(Jan-07-2023, 09:13 AM)jacklee26 Wrote: when i run it occur this error, do you know how to fix this
FutureWarning: save is not part of the public API, usage can give unexpected results and will be removed in a future version
writer.save()
Is not a error just a Warning,if look into it.
pandas.ExcelWriter they have deprecated save() so use close() instead.
Or better as they advice use writer as context manager,then no need to save or close.
with pd.ExcelWriter('out.xlsx', engine='openpyxl') as writer:
    df1.to_excel(writer, 'sheet', index=False)
    df2.to_excel(writer, 'sheet1', index=False)

HI sorry i have one more question to ask,
why is my excel only showing one sheet(sheet1), why is sheet not showing.

I use the print
Quote:print(df1)
print(df2)

it will show
   datetime   name department
1  20220101  James         IT
2  20220101   Test         IT
   datetime   name country
1  20220101  James      US
2  20220101   Test      US
but when opening excel only sheet1 exist, is there any way to also let sheet display.
Reply
#13
(Jan-10-2023, 08:07 AM)jacklee26 Wrote: but when opening excel only sheet1 exist, is there any way to also let sheet display.
The code i did make should have both sheet and and sheet1 in out.xlsx
[Image: cPFfNw.png]
Code and with better names for sheet.
import pandas as pd

with open("pd.txt") as f:
    result = [i.strip('================') for i in f]

# Clean up
s1 = result[:3]
s1 = [i.strip().split() for i in s1]
s2 = result[3:][1:-1]
s2 = [i.strip().split() for i in s2]
# To Pandas
df1 = pd.DataFrame(s1)
df1 = df1.rename(columns=df1.iloc[0]).drop(df1.index[0])
df2 = pd.DataFrame(s2)
df2 = df2.rename(columns=df2.iloc[0]).drop(df2.index[0])
# To Excel
with pd.ExcelWriter('out.xlsx', engine='openpyxl') as writer:
    df1.to_excel(writer, 'sheet1', index=False)
    df2.to_excel(writer, 'sheet2', index=False)
The input pd.txt is just copy of your text file exmple in post 1.
Output:
datetime name department 20220101 James IT 20220101 Test IT ================ datetime name country 20220101 James US 20220101 Test US ================
Reply
#14
Thanks a lot,it work this time. I will try to study this.
Reply
#15
hi i have some questions about the index s1 = result[:3]
what if my file have more than 3 line, if my text file as below
datetime name department
20220101 James IT
20220101 Test IT
20220101 Test IT
20220101 Test IT
20220101 Test IT
20220101 Test IT
================
datetime name country
20220101 James US
20220101 Test US
20220101 Test US
================

it seems like putting a fixed index is not a good way, is there any method without using fixed index.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 214 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Python openyxl not updating Excel file MrBean12 1 339 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 449 Feb-07-2024, 12:24 PM
Last Post: Viento
  Last record in file doesn't write to newline gonksoup 3 437 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,899 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  write to csv file problem jacksfrustration 11 1,547 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,468 Nov-09-2023, 10:56 AM
Last Post: mg24
  Search Excel File with a list of values huzzug 4 1,254 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 851 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,883 Oct-31-2023, 10:21 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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