1 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
您必須使用r'\\t'or '\\\\t',這就是我的做法。
代碼
import pandas as pd
import re
#create the sample dataframe
df = pd.DataFrame({'sent':['13 turned in the research Paper',\
'on Friday; otherwise, he Would',\
'have not passed the Class']})
#df.head()
#apply regex substitution
df['sent'] = df['sent'].astype(str).apply(lambda x: re.sub(r'\s([A-Z][a-z]+$)', r'\\t\g<1>', x))
df.to_csv('tabbed.txt',index=False)
'''
sent
13 turned in the research\tPaper
"on Friday; otherwise, he\tWould"
have not passed the\tClass
'''
#not-so-pretty output
pd.read_csv('tabbed.txt', sep=r'\\t', engine='python')
'''
sent
13 turned in the research Paper
"on Friday; otherwise, he Would"
have not passed the Class
'''
美化輸出
#prettify it
(pd.read_csv('tabbed.txt', sep='\\\\t', engine='python')
.reset_index().rename(columns={'index':'sent0','sent':'sent1'})
.replace(r'"', '', regex=True)
)
'''
sent0 sent1
0 13 turned in the research Paper
1 on Friday; otherwise, he Would
2 have not passed the Class
'''
添加回答
舉報(bào)