Skip to content Skip to sidebar Skip to footer

How To Convert Txt To Excel File

I have a txt file looks like this: Cellname;Ncellname;Technology 52822;13621;GSM; 52822;13622;GSM; 52822;13623;GSM; 52822;16322;UMTS; 52822;16323;UMTS; 52822;16324;UMTS; 52822;1636

Solution 1:

Try this:

import pandas as pd

excel = 'test.txt'

df = pd.read_csv(excel,sep=';')

column_indexes = list(df.columns)

df.reset_index(inplace=True)
df.drop(columns=df.columns[-1], inplace=True)

column_indexes = dict(zip(list(df.columns),column_indexes))

df.rename(columns=column_indexes, inplace=True)

df

and then

df.to_excel('output.xlsx', 'Sheet1')

and in case you don't want the indexes in the output sheet, use this

df.to_excel('output.xlsx', 'Sheet1', index=False)

Solution 2:

import pandas as pd 
file = pd.read_csv('input.txt', sep=';', index=False)
file.to_excel('output.xlsx', 'Sheet1')

Solution 3:

I tried following,

finally what you expected

import pandas as pd

df = pd.read_csv('gsmrelation_mnm.txt', sep = ';', header=None, names=['Cellname', 'Ncellname', 'Technology'])
df.to_excel('gsmrelation_mnm.xlsx', 'Sheet1', index=False, header=None)

Post a Comment for "How To Convert Txt To Excel File"