Python Forum
copy content of text file with three delimiter into excel sheet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
copy content of text file with three delimiter into excel sheet
I have a text file n numbers of rows and columns separated by three delimiters [ , ], - .
I need to import this into excel using python.

sample data of my text file is:

AM[38070] 22220-22-0-0-0-0-1-126-0-0-1-0-0-255-0
AM[38070] 22-0-0-1-1-126-0-0-0-126-0-4095-2047-2047-1
Note there are n numbers of rows and columns i need to write all into excel file

I have tried below code but it only takes one delimiter in consideration

I am not sure what to use instead of row = data[i].split('[')

Below is my code
# mypath should be the complete path for the directory containing the input text files
mypath = raw_input("Please enter the directory path for the input files: ")

from os import listdir
from os.path import isfile, join
textfiles = [ join(mypath,f) for f in listdir(mypath) if isfile(join(mypath,f)) and '.txt' in  f]

def is_number(s):
        return True
    except ValueError:
        return False

import xlwt
import xlrd

style = xlwt.XFStyle()
style.num_format_str = '#,###0.00'

for textfile in textfiles:
    f = open(textfile, 'r+')
    book = xlwt.Workbook()
    ws = book.add_sheet('First Sheet')  # Add a sheet
    data = f.readlines()  # read all lines at once
    for i in range(len(data)):
        row = data[i].split('[')

        for j in range(len(row)):
            ws.write(i, j, row[j])  # Write to cell i, j

        i+=1'.txt', '.xls'))

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 689 Aug-29-2021, 12:39 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 509 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Verification of an excel file saleenahohn 2 453 Jul-09-2021, 06:56 AM
Last Post: Larz60+
  Want to remove the text from a particular column in excel shantanu97 2 629 Jul-05-2021, 05:42 PM
Last Post: eddywinch82
  Read and write active Excel file euras 4 766 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  Open an excel file Newbie1114 1 802 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  Python script for excel sheet Nabil 5 1,010 Jun-01-2021, 02:29 PM
Last Post: anuradha
  Filter Excel and Convert an Excel File giddyhead 0 780 May-13-2021, 06:31 PM
Last Post: giddyhead
  Problems with inserting images into an Excel File FightingFarmer 2 972 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  UPDATE SQLITE TABLE - Copy a fields content to another field. andrewarles 14 1,453 May-08-2021, 04:58 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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