我正在尝试删除大型csv文件中的所有URL,并将其替换为字符串“URL”(所谓的等价标记)。代码执行我想要的操作,但它会将某些行连接在一行中。
这意味着原始csv有63.000行,输出csv只有55000.这不是我想要的。如何用此令牌替换链接并将所有列分开?
#links are replaced with links
import re
with open('data_feat1.csv',"r", encoding="utf-8") as oldfile2, open('data_feat2.csv', 'w',encoding="utf-8") as newfile2:
for line in oldfile2:
line=re.sub(r"http\S+", r"URL", line) #replaces links with "URL"
newfile2.write(line)
newfile2.close()
答案 0 :(得分:0)
解决方案是添加“到”网址“:
line=re.sub(r"http\S+", r'URL"', line) #replaces links with "URL"
我不知道为什么会有效,但确实如此!
答案 1 :(得分:0)