나는 텍스트 파일을 csv 파일로 변환하고 싶습니다. 이름, 날짜, 설명 Im new to python 그래서 누군가가 이것에 대해 나를 안내 할 수있는 적절한 방법을 얻지 못했습니다. 아래는 샘플 텍스트 파일입니다.
================================================== ====
Title: Whole case
Location: oyuri
From: Aki
Date: 2018/11/30 (Friday) 11:55:29
================================================== =====
1: Aki
2018/12/05 (Wed) 17:33:17
An approval notice has been sent.
-------------------------------------------------- ------------------
2: Aki
2018/12/06 (Thursday) 17:14:30
I was notified by Mr. Id, the agent of the other party.
-------------------------------------------------- ------------------
3: kano, etc.
2018/12/07 (Friday) 11:44:45
Please call rito.
-------------------------------------------------- ------------------
np.where(cond, 1, 0).cumsum()
모든 개별 메시지에 태그를 지정 하는 데 사용하십시오 .# read the file with only one col
df = pd.read_csv(file, sep='\n', header=None)
# located the row contains ------ or ======
cond = df[0].str.contains('-----|======')
df['tag'] = np.where(cond, 1, 0).cumsum()
# filter the line contains msg
cond2 = df['tag'] >=2
dfn = df[(~cond & cond2)].copy()
# output
df_output = (dfn.groupby('tag')[0]
.apply('\n'.join)
.str.split('\n', n=2, expand=True))
df_output.columns = ['name', 'date', 'Description']
산출:
name date \
tag
2.0 1: Aki 2018/12/05 (Wed) 17:33:17
3.0 2: Aki 2018/12/06 (Thursday) 17:14:30
4.0 3: kano, etc. 2018/12/07 (Friday) 11:44:45
Description
tag
2.0 An approval notice has been sent.
3.0 I was notified by Mr. Id, the agent of the oth...
4.0 Please call rito.
df :
0 tag
0 ==============================================... 1
1 Title: Whole case 1
2 Location: oyuri 1
3 From: Aki 1
4 Date: 2018/11/30 (Friday) 11:55:29 1
5 ==============================================... 2
6 1: Aki 2
7 2018/12/05 (Wed) 17:33:17 2
8 An approval notice has been sent. 2
9 ----------------------------------------------... 3
10 2: Aki 3
11 2018/12/06 (Thursday) 17:14:30 3
12 I was notified by Mr. Id, the agent of the oth... 3
13 ----------------------------------------------... 4
14 3: kano, etc. 4
15 2018/12/07 (Friday) 11:44:45 4
16 Please call rito. 4
17 ----------------------------------------------... 5
계속해서 이름을 처리 할 수 있습니다.
obj = df_output['name'].str.strip().str.split(':\s*')
df_output['name'] = obj.str[-1]
df_output['idx'] = obj.str[0]
df_output = df_output.set_index('idx')
name date \
idx
1 Aki 2018/12/05 (Wed) 17:33:17
2 Aki 2018/12/06 (Thursday) 17:14:30
3 kano, etc. 2018/12/07 (Friday) 11:44:45
Description
idx
1 An approval notice has been sent.
2 I was notified by Mr. Id, the agent of the oth...
3 Please call rito.
더 많은 헤더 열 추가 :
cond = (df['tag'] == 1) & (df[0].str.contains(':'))
header_dict = dict(df.loc[cond, 0].str.split(': ', n=1).values)
# {'Title': 'Whole case',
# 'Location': 'oyuri',
# 'From': 'Aki ',
# 'Date': '2018/11/30 (Friday) 11:55:29'}
for k,v in header_dict.items():
df_output[k] = v
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다