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

How do I make pip install binaries to a writable path?
TensorFlow: generating a random constant
How can I organize my functions to get something like class.draw.line() instead of class.drawLine
Traceback (most recent call last): (works in trinket.io but not 3.4/2.7.11)
python : Mode of a List
How to send and receive data from flask?
Python HTTPS Login to account to scrape data, is this bad practice?
Celery, redis and ConnectionPool
Numpy Choose Elements from 2 arrays
Selenium Python iterate over a table of rows it is stopping at the first row
How to add a variable of a method of a class in aonther program in python?
Django : Getting error while removing django.contrib.sites from INSTALLED_APPS
Can I scrape a html page which is local machine using scrapy?
how to get request HTTP headers in soaplib views file?
Hive transform query runs slow with ORC file
What if I *really* need to escape quotes for an SQL script?

Categories

HOME
makefile
signalr
wmic
ncurses
memory-leaks
transparent
matplotlib
wampserver
rebol
gspread
jpanel
sudo
diagram
oclint
finite-group-theory
data-synchronization
mongodb-query
future
glpk
react-leaflet
sendkeys
titan
google-people
google-api-java-client
cruisecontrol.net
autolayout
thingsboard
piwik
bootstrap-typeahead
badge
net-snmp
graph-databases
swiftcharts
nsurlconnection
ios-ui-automation
oscommerce
es-shell
codesys
code-composer
fastq
production-environment
jenkins-jira-trigger
xmgrace
rollback
ogre3d
maintenance
hpcc
jszip
unity3d-editor
sql-import-wizard
cfeclipse
rmongodb
glkit
angularjs-ng-pattern
linkageerror
image-editing
emokit
console-redirect
selecteditem
usb-drive
cmocka
spservices
schtasks.exe
geodjango
optionbutton
datasnap
textkit
subresource-integrity
apache-mina
stackframe
odftoolkit
hidden-field
vcl
mov
reserved-words
db4o
edit-in-place
winrt-httpclient
arangodb-php
facebook-chat
bfd
real-time-updates
dig
fortran77
mvs
mozart-mvc
android-4.0
enumerators
xgettext
fotoware
z-machine
dcpu-16
search-path
squeel
hadoop-plugins
sitemappath
communicationexception
front-controller
asp.net-mvc-controller
self-reference

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