#확장자 txt 탐색: cmd; dir/b *.txt>txt_list.txt
import pandas as pd
from tqdm import tqdm
## 불러올 파일명 목록 호출
with open('txt_list.txt', 'r', encoding = 'cp949') as f:
data = f.readlines()
# 각각의 파일 불러와서 title column 및 해당되는 값 추가
for data_a in tqdm(data): #data_a = txt 파일의 파일명
with open(data_a.strip()+'.txt', 'r', encoding = 'cp949') as f1:
data_aa = f1.readlines()
data_split = [x.strip().split('|') for x in data_aa[0:]]
df = pd.DataFrame(data_split, columns = ['이름','번호','비고'])
df['파일명'] = data_a.strip()
df = df.apply(lambda x: x.str.strip(), axis = 1) #각각의 data point에서 공백 제거.
df = df[['파일명','비고','이름', '번호']]
df.to_csv('C://Users//DIQUEST//Desktop//STT_합치기//변환//'+data_a.strip()+'.txt',index=False,sep='\t', \
encoding='cp949')
import os
directory = "C://Users//DIQUEST//Desktop//STT_합치기//변환" # 작업할 txt 파일들이 담겨있는 폴더 위치 지정
outfile_name = "stt_data_merged.txt" # 결과 파일명 지정
out_file = open(outfile_name, 'w', encoding = 'cp949') # 결과 파일 생성
input_files = os.listdir(directory) # 폴더 내용물 목록 생성
# 각각의 txt 파일 불러와서 합치기
for filename in tqdm(input_files):
if ".txt" not in filename:
continue
file = open(directory + "/" + filename, encoding = 'cp949')
content = file.read()
if '파일명\t비고\t이름\t번호\n' in content:
content = content.replace('파일명\t비고\t이름\t번호\n','')
else:
pass
out_file.write(content)
file.close()
out_file.close()
# txt -> excel로 변환
with open('stt_data_merged.txt', 'r', encoding = 'cp949') as f2:
data2 = f2.readlines()
data_split2 = [x.strip().split('\t') for x in data2[0:]]
df2 = pd.DataFrame(data_split2, columns = ['파일명','비고','이름', '번호']) #타이틀 추가
df2.to_csv('stt_data_merged_complete.txt',index=False, sep='\t', encoding='cp949') #txt
df2.to_excel('stt_data_merged_complete_excel2.xlsx', index=False) #excel