SoFunction
Updated on 2024-11-14

python using pandas to convert excel file to txt file method

python will change the data to txt there are many ways to use xlrd library to achieve. I am lazy, do not want to press too much use less plug-ins, the use of existing libraries pandas will be converted to excel file txt file.

Straight to the code:

'''
function: convert excel file to text
author: Nstock
date:2018/3/1
'''
import pandas as pd
import re
import codecs
 
#Convert excel to txt file
def exceltotxt(excel_dir, txt_dir): 
 with (txt_dir, 'w', 'utf-8') as f:
 neg=pd.read_excel(excel_dir, header=None, index=None)
 (neg.to_string())
 
# Remove numbers and spaces from the beginning of a record line
def del_linehead_number_speace(orig_txt_dir,saveas_txt_dir):
 with open(orig_txt_dir,'r+') as f, open(saveas_txt_dir,'r+') as fw:
 lines = ()
 print(len(lines)) # of lines
 texts = [(r'(\d)+(\s)+','',lines[num]) for num in range(len(lines)) ]
 
 texts = list(set(texts)) # de-duplicate if you want to keep duplicate records comment the line
 
 line_num = len(texts)
# for num in range(line_num): # see the transformed text
#  print(texts[num])
 (texts)
 
exceltotxt('./data/', './data/neg_temp.txt') 
del_linehead_number_speace('./data/neg_temp.txt','./data/')

Description:xxx_dir with target filename is: xxx_dir='save path/'+'filename'

The above this python using pandas to convert excel file to txt file is all I have to share with you, I hope to give you a reference, and I hope you support me more.