Python Forum
write to excel will be empty in column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write to excel will be empty in column
#1
i have a question about reading a text file, but in the text file occur many empty space.
My text file data_out.txt:
no mac mta MAC ETH MAC
=== =========== =========== ===========
2 7719B1822090 7719B1822091 7719B1822092
3 749111111210 491111112101 491111112102

My code:
import openpyxl,string 
f=open(r'D:\test\data_out.txt','r+') #open text 
#########if load excel file ########################
#excel=openpyxl.load_workbook(r'D:\\test\\test.xlsx') #open excel 
#excel=openpyxl.load_workbook(r'D:\\test\\test.xlsx') #open excel 
excel = openpyxl.Workbook() 
sheet=excel.worksheets 
line=f.readline(); #read text 

while line: 
    list=[] 
    list = line.split(sep=' ') #convert, 
    print(list)
    for i in range(0, len(list)): # remove space 
        
        #list[i] = list[i].strip('\n') 
        list[i] = list[i].strip('\n')
        #print(list[i])
    sheet[0].append(list) #wrire into excel 
    
    line=f.readline() #read next line 
    print(line)
excel.save(r'D:\test\test.xlsx')


When it write to my excel file, the column on c3, c4, c5,c6, d3,d4,d5,d6 will be empty.
How can i let G3 move to c3, and G4 move to c4.
is there any way

it will print
['no', '', '', '', '', '', '', '', '', '', '', 'mac', '', '', '', '', '', '', '', '', 'mta', 'MAC', '', '', '', '', '', '', '', '', '', 'ETH', 'MAC', '\n']
['===', '', '', '', '', '===========', '', '', '', '', '', '===========', '', '', '', '', '===========', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '\n']
['2', '\t7719B1822090', '', '', '', '', '7719B1822091', '', '', '', '', '7719B1822092\n']
['3', '\t749111111210', '', '', '', '', '491111112101', '', '', '', '', '491111112102\n']
['\n']


how to remove the ''?
Reply
#2
one thing I notice immediately that will get you in trouble:
never name a list list.
that overwrites python's list
example:
>>> z = ('a','b','c')
>>> # following is legal
>>> zz = list(z)
>>> zz
['a', 'b', 'c']
>>> # following is taboo
>>> list = [1,2,3]
>>> z = ('a','b','c')
>>> zz = list(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>>
Reply
#3
do you know how to write == ======= ======= ======= to excel
Reply
#4
I rarely output any data to excel, usually use pandas, or some other format.
Please provide a sample data_out.txt file.
Reply
#5
Below is data_out.txt file

no mac mta_MAC ETH_MAC
=== =========== =========== ===========
2 7719B1822090 7719B1822091 7719B1822092
3 749111111210 491111112101 491111112102

if not add === =======, it will write to excel, but if add it open excel will pop formula problem
i think maybe xcel think === it is formula
Reply
#6
Pandas read it fine,just need to remove first row with ===.
Guess i could get to work fine in openpyx to,but not looked into that now.
import pandas as pd

df = pd.read_clipboard() # Copy of your data
df = df.drop(df.index[0])
df.to_excel('mac.xlsx', index=False)
Output:
no mac mta_MAC ETH_MAC 2 7719B1822090 7719B1822091 7719B1822092 3 749111111210 491111112101 491111112102
Reply
#7
hi snippsat,
Can we also add === into excel
Reply
#8
(Jun-26-2020, 11:35 PM)jacklee26 Wrote: Can we also add === into excel
Can leave it in bye comment out line 4,but in excel it will now be 0.
Output:
no mac mta_MAC ETH_MAC 0 0 0 0 2 7719B1822090 7719B1822091 7719B1822092 3 749111111210 491111112101 491111112102
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I write all csv records to Excel ? Revox 2 964 Mar-29-2023, 03:53 PM
Last Post: deanhystad
  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
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,130 Jan-28-2023, 12:53 PM
Last Post: Winfried
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,051 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Openpyxl manipulate excel write formula SamLiu 0 1,063 Nov-04-2022, 03:00 PM
Last Post: SamLiu
  How to format Excel column with comma? dee 0 1,376 Jun-13-2022, 10:11 PM
Last Post: dee
  Cursor write 3rd file empty paulo79 3 1,903 Mar-10-2022, 02:51 PM
Last Post: DeaD_EyE
  Appending Excel column value as CSV file name sh1704 0 1,304 Feb-06-2022, 10:32 PM
Last Post: sh1704
  dataframe write to tab delimited differs from Excel koh 0 2,009 Aug-01-2021, 02:46 AM
Last Post: koh
  Want to remove the text from a particular column in excel shantanu97 2 2,150 Jul-05-2021, 05:42 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

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