2017-05-25 1053 views
-1

我想减少我的推特语料库的功能。出于这个原因,我打算用一个等价类标记替换用户名。用户名的特征是以@开头。我尝试使用re.sub(),但它不按预期工作。它取代了句子中的名字,而不是在句子的开头。哪里不对?用“USERNAME”替换twitter用户名(@ ...) - 如何?

#usernames (e.g. @max) are replaced with An equivalence class token 

import re 
with open('outfilename2.csv',"r", encoding="utf-8") as oldfile1, open('outfilename3.csv', 'w',encoding="utf-8") as newfile1: 
    for line in oldfile1: 
     line=re.sub(r"(\s)@\w+", r" USERNAME", line) 
     newfile1.write(line) 
newfile1.close() 

回答

2

你的正则表达式是错误的,你自称想要什么要做到:

line=re.sub(r"\[email protected]\w+", "USERNAME", line) 

如果你想匹配@anything_anywhere其中@由非边界字符开头且USERNAME更换。

-1

线= Line.split( “{”)[1] .split( “}”)[0] 这可能有助于

+1

tweets中的用户名不在括号内,但是它们是以@开头的正常文本。 –

+0

示例推文:“我去购物@sarah。” 目标:“我去购物USERNAME”。 –

+0

Username.upper()使其成为大写 – IsaBostan

相关问题