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

Error on assigning a value to a Rectangle in Pygame
Two python/pygame programs to run in the same window
Django Haystack and Elasticsearch Error Using rebuild_index
Can't keep a total of numbers in python
replace if else with a for loop
Panda3D - Setting date
decoding json in python
Python debugging, stop at particular output
Why an avi file can not be opened in OpenCV-Python
Can't compile msgpack Python extension under windows
Python sockets for a n-players game
HTMLParser or urllib2 unicode issue
How to stop PyCharm inserting spaces for fine alignment when reformating code?
Install NLTK with IronPyton for VS2012
pip install won't install on my machine
Integrating using scipy.integrate.simps

Categories

HOME
jdbc
pug
nuxeo
deep-linking
playframework
h2
rocketmq
specflow
webdav
laravel-4
jrebel
netflix-feign
packer
dryioc
cpanel
local
squarespace
cratedb
symfony2-easyadmin
react-leaflet
cross-platform
aspell
riak-ts
ms-access-2007
google-api-java-client
propel2
source-maps
widevine
xilinx-ise
dd-wrt
superscript
protobuf-net
apache-cloudstack
pmwiki
android-download-manager
deepstream.io
web-development-server
react-native-router-flux
homekit
powermta
worker
php-ews
ideone
chunked-encoding
amazon-machine-learning
web-deployment-project
istorage
pluck
tcserver
search-box
adler32
coremidi
tuxedo
picking
except
cctray
document-oriented-db
hornetq
retro-computing
programming-paradigms
webhdfs
risk-analysis
type-mismatch
swift2.1
callstack
jazz
consensus
pgagent
method-overriding
planetary.js
dbsetup
zipalign
bsod
modeshape
driver-signing
image-zoom
trimming
haskell-platform
visual-studio-2003
ienumerator
manage.py
symphony-cms
pep8
marmalade-edk
maven-ear-plugin
legacy-code
v4l
fileutils
opengl-es-lighting
nagle
.app
revert
communicationexception
front-controller
swfloader
winsnmp
associativity

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