1 回答
TA贡献1816条经验 获得超6个赞
您必须使用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
'''
添加回答
举报