-1

I have file with lacks of line of data in excel. I want to process those by converting in text file and then creating 3 excel files from that original 1. I need help to convert txt file to excel and vice versa in Python. Kindly help me.

prasad-jpg
  • 21
  • 1
  • 5
  • You could use the excel library or essentially any library which can read from files and then split the data and again save it to desired file – theProcrastinator Mar 03 '21 at 04:32

1 Answers1

0

I am using openpyxl to format the xls file. But, for example, I wrote a simple code to demo

dfsource - this can be a text file read converted to a list from any option

dfsource=open('text.txt').read().split('\n')

dfsource=dfsource if isinstance(dfsource,list) else [1,2,3,4,5,6,7,8,8,0]

next, read json/dict/list into xls

import pandas as pd
import os


dfsource = [
    'Timestamp;T;Pressure [bar];Input line pressure [bar];Speed [rpm];Angular Position [degree];Wheel speed [rpm];Wheel angular position [degree];',
    ';1;5,281;5,303;219,727;10,283;216,363;45;',
    ';1;5,273;5,277;219,727;11,602;216,363;45;',
    ';1;5,288;5,293;205,078;12,832;216,363;45;',
    ';1;5,316;5,297;219,727;14,15;216,363;45;',
    ';1;5,314;5,307;219,727;15,469;216,363;45;',
    ';1;5,288;5,3;219,727;16,787;216,363;45;',
    ';1;5,318000000000001;5,31;219,727;18,105;216,363;45;',
    ';1;5,304;5,3;219,727;19,424;216,388;56,25;',
    ';1;5,291;5,29;219,947;20,742;216,388;56,25;',
    ';1;5,316;5,297;219,507;22,061;216,388;56,25;']
mat = [n.split(';') for n in df]

df = pd.DataFrame(mat)

namefile='abc'

relative_filename = os.path.join(
    # 'downloads',
    '{}-download.xlsx'.format(namefile)
)
absolute_filename = os.path.join(os.getcwd(), relative_filename)
writer = pd.ExcelWriter(absolute_filename)
df.to_excel(writer, 'Sheet1')
writer.save()
Blay Wille
  • 46
  • 1
  • 5