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

Copy C structure to Python List
python error:List index out of range
While loop inside for loop?
Serialize multiple models in a single view
Iterating over a list adding attributes to an object in Python [duplicate]
Fourier Transformation vs Numpy FFT
pandas to_html no value representation
Using Pandas To Find the Number Of Periods Since the Rolling High
List Comprehension For Loop + Ternary Operation For Loop?
Pass variable to multiple templates in pybottle
Next and Before Links for a django paginated query
Python - String of Digits to Integer using Recursion?
Weird type error arising when I add to a database using SQL Alchemy
Python for loop causes system crash
Class object variable = none
Print last <td> in beautiful soup

Categories

HOME
rest
cil
npm
jsviews
voip
wxwidgets
mapbox-gl-js
amazon-product-api
extract
travis-ci
jrebel
xtext
sudo
ng-admin
desktop
ipfs
xamarin-studio
finite-group-theory
cs-cart
cratedb
glpk
multiple-monitors
alfresco-share
zoho
tweets
jni
countif
servicemix
npm-install
croppic
hash-collision
widevine
web-frontend
vertex-buffer
svnkit
entity-system
google-drive-realtime-api
npm-shrinkwrap
transient
bing-maps-api
topic-modeling
silverlight-3.0
viewgroup
xbim
google-account
excel-interop
dojox.mobile
stress-testing
worker
facebook-chatbot
livefyre
sapui
persistent
android-bitmap
pdfkit
verbose
pdflib
rmongodb
debugdiag
glkit
mailcatcher
brython
ng2-material
dac
django-south
concur
persistence.xml
windows-vista
wikitext
selendroid
textkit
mogrify
stream-framework
sysfs
android-search
belongs-to
candidate-key
ghostdoc
dalekjs
code-testing
ssms-addin
joox
slick-2.0
reserved-words
fogbugz-api
loop-invariant
ng-hide
backbone-relational
argb
objcmongodb
android-contextmenu
autostart
deploying
ninject-extensions
z-machine
applicationcontext
responsetext
entity-framework-ctp5
scraperwiki
qtembedded
gtk2hs
dashcode
email-spec
delegatecommand
microsoft.ink
revision
longjmp
dirty-data
inline-if
writing

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