I'm using python to deal with multiple text in excel, and I need to input the text at sheet1 and split the data in sheet2.
The text is something like. text file The text is including a lot of delimiter e.g.#,~ and two space, one space. so I tried to input them first
import pandas as pd
import glob
import os
path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.txt"))
for a in csv_files:
print(a)
data_list = []
for file in csv_files:
data_list.append(pd.read_csv(file))
print(data_list)
sheet1 = pd.concat(data_list, axis=0, sort=True)
sheet1.to_excel('test1.xlsx', encoding='utf-8',index=True,header=True)
I don't know how the split them first so I tried to put them in one sheet, and create the dataframe and split them.
df2=pd.DataFrame()
df2=sheet1["#xpost 2"].str.replace('#',' ').str.replace(' +',' ').str.lstrip().str.split(' ', expand=True)
with pd.ExcelWriter("test1.xlsx", mode="a", engine="openpyxl",if_sheet_exits="new") as writer:
df2.to_excel(writer,sheet_name='sheet1',index=True,header=True,)
finally I got the results as image, have same index for each text result, but that is not I want. result
I'd like my output to look like this, split them by multiple delimiter and arrange them by columns automatically. output
Is there have more elegant way of doing this? Thanks a lot.