使用Python移除URL/@等内容

我刚开始学习Python和编程。我有一个文本文件,里面包含了一些URL/@/#等内容,我想去除这些内容以获得干净的文本数据,供机器学习算法使用。例如,文本数据如下所示,

@Su2ieQ13 But you're IMing with meeeeee. "@apogeum whoooaa, thats soo awesome  my eyes look like black.. except if you have a yellow light bulb close to my eyes then u can"The shop of the day  http://"i couldn't sleep so i stayed awake watching @lilbsuremusic on this live stream thingy and now i'm taking my butt to bed, so sweet dreams "@Lee_Knight ok haha thanks i will try that lol 

我编写的代码如下,

import reimport string# load text negativefilename_neg = '/path/to/my/text_file'file = open(filename_neg, encoding="ISO-8859-1")text_neg = file.read()text_neg = re.sub(r'^https?:\/\/.*[\r\n]*', '', text_neg,flags=re.MULTILINE)file.close()# split into words by white spacewords_neg = text_neg.split()print(words_neg)

但我仍然无法移除URL等内容。如果有人能帮我解决这个问题,我将不胜感激。谢谢。


回答:

text_neg = re.sub('@|http://|"', '', text_neg,flags=re.MULTILINE).

你想移除的符号应该用|分隔。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注