python


How to replace a pattern in a string?


Hi I am trying to replace all the expressions containing 'www...' and 'http://..' with just 'URL'. I tried this but I am getting this error.
TypeError: expected string or buffer
My code is:
df['text_1'] = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','URL',df['text'])
df[text] contains tweets, so I want to keep only the text in there.
I am in Python 2
Thanks.
Assuming df is a pandas DataFrame, don't use re.sub. Use pandas.DataFrame.replace instead:
df['text_1'] = df['text'].replace('((www\.[^\s]+)|(https?://[^\s]+))',
'URL',
regex=True)
This will generate a new column text_1 with all values of text replaced according to your regular expression.
It sounds like you're getting that error because you're not supplying a string or buffer as the third argument to re.sub.
>>> re.sub('\W', 'REPLACED', 'this is my text')
'thisREPLACEDisREPLACEDmyREPLACEDtext'
>>> re.sub('\W', 'REPLACED', None)
Traceback (most recent call last):
...
TypeError: expected string or buffer
Ensure that df['text'] contains a proper string before you try using it for re.sub

Related Links

Why is Python not setting up my cron job's time properly?[Solved]
Training Data Set in NLTK Python
Pip3 packages can't be found/used by the terminal
No recipients have been added when trying to send message with Flask-Mail
Get 503 response code from youtube, when scraping comments with python
Pip can't upgrade a built-in package in ubuntu 15.04
PyRun_SimpleString is blocking whenever import is used, never returns
What is the purpose of setuptools requirements of the form “package===version”
Django admin site not opening while running from pydev in eclipse
WebdriverWait is showing TimeoutException, if i use sleep.time it works ok
Python: Create Array from Indexed List
Overlaying n (user generated) points over a graph
How to get django model object in save method
How to pass file pointer in dll routines using ctypes in python
unable insert jpeg into filemaker db with pyodbc : HY011 ODBC Error
How to pass parameter as pointer in Python [duplicate]

Categories

HOME
jsf
crystal-reports
cntk
shopify
npm
meshlab
windows-7
ocaml
agile
checksum
ios10
hyperledger-fabric
simpy
actionscript-2
structuremap
outlook-web-addins
iso
game-maker-studio-1.4
cosmicmind
squarespace
datagrip
mule-studio
bar-chart
python-unicode
grub2
saiku
derived
tree-traversal
chocolatey
tortoisegit
jni
sql-update
rails-activerecord
google-maps-android-api-2
scalaz7
go-cd
spring-test
branch
guile
ab-initio
multichoiceitems
superscript
spring-insight
fabric-digits
recurrence-relation
sidr
discord.net
android-download-manager
elfinder
dql
topic-modeling
concrete5-5.7
fltk
viewgroup
excel-interop
homekit
openweathermap
estimation
automator
taskmanager
production-environment
istorage
photon-controller
react-native-fbsdk
createobject
picking
logcat
bluesnap
brackets-shell
magento-1.4
web-essentials
android-recyclerview
hateoas
self-hosting
selendroid
textkit
subresource-integrity
abas
bridge
rspec3
web-component-tester
mdm-zinc
guzzle6
es2015
java-melody
docopt
facebook-wall
dache
smartystreets
mfmailcomposeviewcontroll
ifft
broadcasting
openkinect
internal
exiv2
angulartics
installshield-2011
ng-hide
client-side-scripting
lync-server-2010
icefaces-3
plone-funnelweb
getstring
jquery-selectbox
executable-format
sendfile
pivotal-crm
rijndael
pureftpd
getresource
carbide
chdatastructures
longjmp
nt4

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App