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

NLTK and Stanford parsing
How can I apply numerical gradient in Tensorflow?
How can I generate categorical synthetic samples with imblearn and SMOTE?
Writing a function to initialize classes with uncertain input?
Batch Rendering file from a python script without openeing Maya
Python hex to binary fail
Post and redirect to cross domain URL
Use Unicode text with seaborn
If Loop - Index out of range
Making a Turtle Object Look Look Further for Another Object
Facebook Bot sending reply multiple times
DNS query forwarding if local DNS is resolving
Loading flask configuration using from_object
Python OrderedDict with lambda [duplicate]
TypeError: unorderable types: dict() > dict()
How to install python sipconfig module on win7

Categories

HOME
google-apps-script
ember.js
admin-on-rest
npm
textwatcher
puppet
docker-swarm
chaiscript
jscript
phonegap-cli
jpanel
i2c
shader
slim-3
google-classroom
keystore
intellij-plugin
symfony-forms
postmessage
multiple-monitors
rapidjson
lstm
r-lavaan
android-toolbar
grub2
chocolatey
thumbnails
ip-camera
spring-ws
source-maps
profile
delete-file
myob
yosys
tampermonkey
hanami
opencart2.3
net-snmp
google-api-dotnet-client
fileinfo
pcre
gpib
referenceerror
piecewise
protobuf-net
outsystems
fractals
swiftcharts
node-horseman
mapguide
boilerplate
ntfs-mft
parent
control-flow-graph
ivona
timesten
floor
dimple.js
goquery
pycurl
uicollectionviewlayout
python-hypothesis
emoticons
laravel-query-builder
pspice
libtiff.net
sql-import-wizard
gradle-script-kotlin
zurb-foundation-apps
freetype2
multifile-uploader
spatial-query
picking
eml
asp.net-mvc-2
xmp
magento-1.4
loopj
geodjango
vips
anti-patterns
playscape
git-checkout
hg-git
temp-tables
gyroscope-framework
and-operator
dmp
cg
genetic-programming
dto
csplit
wss
level
indexing-service
unison
svcutil.exe
bigint
cdata
backbone-relational
zend-lucene
device-emulation
fieldset
snapjs
zend-framework-modules
sequelpro
enumerators
vim-powerline
pstree
law-of-demeter
flash-cs5.5
viewdidload
surf
hadoop-plugins
plinq
kpi
soft-keyboard

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