python - 从python中的csv文件中的推文中删除不需要的单词(字符)
问题描述
我有一个包含 60000 多条推文的 csv 文件。我已经在一定程度上清理了文件。但它仍然有没有任何意义的单词(在 url 清理后可能会遗漏混合字符)。我不允许发布任何图像。所以,我发布了文件的一部分。"""
Fintech Bitcoin crowdfunding and cybersecurity fintech bitcoin crowdfunding and cybersecurity
monster has left earned total satoshi monstercoingame Bitcoin
Bitcoin TCH bitcoin btch
bitcoin iticoin SPPL BXsAJ
coindesk The latest Bitcoin Price Index USD pic twitter com aKk
Trends For Bitcoin Regulation ZKdFZS via CoinDeskpic twitter com KNKgFcdxYD
Now there Mike Tyson Bitcoin app theres mike tyson bitcoin app
BitcoinBet Positive and negative proofs blockchain audits Bitcoin Bitcoin via
The latest Bitcoin Price Index USD pic twitter com CivXlPj
Bitcoin price index pic twitter com xhQQ mbRIb
如您所见,某些字符(例如,aKk、KNKgFcdxYD、xhQQ)没有任何意义,所以我想删除它们。它们存储在名为 [clean_tweet] 的列中。
为了整个清洁目的,我将以下代码缝合在一起(从原始推文到我发布的当前版本),但不知道如何删除这些“字符”。我的代码如下。任何建议,将不胜感激。谢谢你。
import re
import pandas as pd
import numpy as np
import string
import nltk
from nltk.stem.porter import *
import warnings
from datetime import datetime as dt
warnings.filterwarnings("ignore", category=DeprecationWarning)
tweets = pd.read_csv(r'myfilepath.csv')
df = pd.DataFrame(tweets, columns = ['date','text'])
df['date'] = pd.to_datetime(df['date']).dt.date #changing date to datetime format from time-series
#removing pattern from tweets
def remove_pattern(input_txt, pattern):
r = re.findall(pattern, input_txt)
for i in r:
input_txt = re.sub(i, '', input_txt)
return input_txt
# remove twitter handles (@user)
tweets['clean_tweet'] = np.vectorize(remove_pattern)(tweets['text'], "@[\w]*")
#remove urls
tweets['clean_tweet'] = np.vectorize(remove_pattern)(tweets['text'], "https?://[A-Za-z./]*")
## remove special characters, numbers, punctuations
tweets['clean_tweet'] = tweets['clean_tweet'].str.replace("[^a-zA-Z#]", " ")
#
tweets['clean_tweet'] = tweets['clean_tweet'].apply(lambda x: ' '.join([w for w in x.split() if len(w)>2]))
解决方案
限定你想要的字符而不是不需要的字符的宇宙可能更容易。与正则表达式的负匹配?
if (re.match(r'[A-Za-z0-9@#$%^&*()!-+='";:?', char) is not None) is False:
replace(char, '')
为您要查找的内容稍微清理一下类似正则表达式的内容,然后循环遍历每个字符串的字符。然后,感谢上帝让计算机为您完成所有单调乏味的工作!
推荐阅读
- python - How to plot this type of graph in Python Pandas
- python - 我如何知道每个工人的可用内存?
- powershell - 在单行中产生输出
- html - 如何修复CSS渐变背景在移动浏览器的底部留下空白
- windows - 为什么 Chromium 会阻止 DefaultSearchProviderEnabled 组策略?
- r - Excluding both the minimum and maximum value
- python - 为什么 SQLite 中的多列索引会降低查询的性能,除非索引所有列?
- sql - 当有人提交表单时,您如何编写脚本来自动填写其他表格?
- java - Xpath 创建问题
- python - 为什么这种递归不会超过递归限制