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
#1
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):
    try:
        float(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
    book.save(textfile.replace('.txt', '.xls'))
    f.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Verification of an excel file saleenahohn 2 142 Jul-09-2021, 06:56 AM
Last Post: Larz60+
  Want to remove the text from a particular column in excel shantanu97 2 227 Jul-05-2021, 05:42 PM
Last Post: eddywinch82
  Read and write active Excel file euras 4 353 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  Open an excel file Newbie1114 1 326 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  Python script for excel sheet Nabil 5 454 Jun-01-2021, 02:29 PM
Last Post: anuradha
  Filter Excel and Convert an Excel File giddyhead 0 395 May-13-2021, 06:31 PM
Last Post: giddyhead
  Problems with inserting images into an Excel File FightingFarmer 2 431 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  UPDATE SQLITE TABLE - Copy a fields content to another field. andrewarles 14 744 May-08-2021, 04:58 PM
Last Post: ibreeden
  Copy column from one existing excel file to another file mkujawsk 0 469 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Importing text file into excel spreadsheet with formatting david_dsmn 1 455 Apr-05-2021, 10:21 PM
Last Post: david_dsmn

Forum Jump:

User Panel Messages

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