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

PyYAML YAML load_all to Objects (nicely)
ttf_lookup_cache attribute not found in FontManager when using plot()
How to sort keys of dict by values?
a data structure with key as a cluster of words and value as a single word or string
Disable python warnings only for specific module via commandline
Error running GAE tutorial code - Guestbook
How to create a running total- Python [closed]
Unbuffered Python Subrocess PIPE
Django runserver syntax error
How to parse the string into list of tuples
Make instance methods global in module?
try-finally with SqlAlchemy: is this a good habit?
Bokeh not handling colors properly with ColumnDataSource
Psychopy MovieStim3: pausing errors
Django Mysql Complex annotations require an alias
Django: Unresolved reference 'project_name' warning

Categories

HOME
backup
wxwidgets
smartphone
otrs
memory-leaks
sbt-assembly
apiconnect
subquery
netflix-feign
vuex
jboss-eap-7
public-key-encryption
cs-cart
symfony2-easyadmin
rapidjson
medium-editor
jcl
ms-access-2007
jogl
primes
apply
explode
go-cd
philips-hue
rhmap
mef2
pentaho-report-designer
partial-application
gzip
blackboard
roundcube
gpib
freebase
rhino
large-data
threshold
powershell-remoting
sidr
codesys
issue-tracking
sonarqube-scan
concrete5-5.7
cross-entropy
common.logging
onbackpressed
xbim
finite-element-analysis
prototypejs
windows-firewall
appstore-approval
jenkins-jira-trigger
webdatagrid
paas
collapsingtoolbarlayout
visual-studio-templates
gmt
iron.io
shinyjs
rsqlite
hspec
vst
bacnet
faraday
console-redirect
aerogear
qt-linguist
web-essentials
acm
lemoon
selendroid
windowlistener
base32
integral
twitter-rest-api
coypu
gray-code
pylearn
pgagent
jquery-autocomplete
cg
genetic-programming
sid
facebook-wall
argument-passing
factors
bounds
cdata
sslexception
driver-signing
visual-studio-2003
multiple-conditions
fluidsynth
vt100
snapjs
icsharpcode
radchart
cosm
concurrent-programming
visual-web-gui
maven-ant-tasks
objectbrowser
gtk2hs
cookieless
clause
chatroom
winverifytrust
inline-if
msf
database-diagramming
ajaxpro





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android


MS Developer
developer works
python
ios
c
html
jquery


RDBMS discuss