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

Converting multi-line String to Dict in Python
passing structure from c++ to python
MySQL string replace using SQLAlchemy
How to copy an image from server directory to another directory in python
Python Selenium click on a specific row in a table containing the right data in a column
How to run different methods on strings through a variable?
Why does order of comparison matter for this apply/lambda inequality?
python returning to the default statement
How can 2 objects be considered not equal and not identicial by Python, but have the same id?
No post request after submitting a form when I want to download a PDF
How to divide some csv files into two parts, and then merge them as an csv file in python
Can't popen new instance of python.exe
What am I doing wrong with requests in python: ValueError: Expecting value: line 1 column 1 (char 0)?
How can I set the value of a Field() that is not being populated by Scrapy
XML not parsing as expected with BeautifulSoup
Move 3D plot to avoid clipping by margins

Categories

HOME
cluster-computing
freepascal
docker-swarm
windows-store-apps
angular2-directives
proguard
google-spreadsheet-api
lotus-notes
hashicorp-vault
cross-platform
scaling
saiku
atlassian-plugin-sdk
jpeg2000
esql
widevine
fileinfo
gpib
referenceerror
salesforce-chatter
ios-ui-automation
hashcat
bigcartel
liquid-xml
intersystems-ensemble
upsert
ttcn
ivona
3scale
azure-availability-set
nsjsonserialization
range-v3
appstore-approval
jenkins-jira-trigger
multi-agent
static-code-analysis
mercury
python-hypothesis
lotus
efxclipse
pspice
gherkin
kendo-combobox
squirrel
project-online
servlet-3.0
matcaffe
connection-reset
ejabberd-saas
tilestache
qtestlib
odftoolkit
dto
ember-addon
bsp
phpdocx
codeigniter-a3m
data-generation
db4o
visual-studio-2003
nuspec
symphony-cms
fortran77
htmltextwriter
device-emulation
qt4.6
process-monitor
transitive-closure-table
lync-server-2010
signals2
userid
msgbox
selectonemenu
phpcrawl
post-redirect-get
j-interop
revert
cots

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