53👍
✅
Split on any whitespace, then join on a single space.
' '.join(s.split())
20👍
>>> import re
>>> re.sub(r'\s+', ' ', 'some test with ugly whitespace')
'some test with ugly whitespace'
- [Django]-How to reset migrations in Django 1.7
- [Django]-Setting default value for Foreign Key attribute in Django
- [Django]-Can the Django ORM store an unsigned 64-bit integer (aka ulong64 or uint64) in a reliably backend-agnostic manner?
7👍
I would use Django’s slugify
method, which condenses spaces into a single dash and other helpful features:
from django.template.defaultfilters import slugify
- [Django]-Passing objects from Django to Javascript DOM
- [Django]-How to check ImageField is empty
- [Django]-Check if model field exists in Django
1👍
"electric guitar".split()
will give you ['electric', 'guitar']
. So will "electric \tguitar"
.
- [Django]-Supervising virtualenv django app via supervisor
- [Django]-Django + apache & mod_wsgi: having to restart apache after changes
- [Django]-Django model default sort order using related table field
-2👍
This function removes everything which is not digit in a string. I use it all over the place.
def parseInt(string):
if isinstance(string, (str, int, unicode)):
try:
digit = int(''.join([x for x in string if x.isdigit() ]))
except ValueError:
return False
else:
return digit
else:
return False
- [Django]-Is there a function for generating settings.SECRET_KEY in django?
- [Django]-Aggregation of an annotation in GROUP BY in Django
- [Django]-Disable migrations when running unit tests in Django 1.7
-10👍
There could be many white spaces like below:
var = " This is the example of how to remove spaces "
Just do simple task like, use replace function:
realVar = var.replace(" ",'')
Now the outpur would be:
Thisistheexampleofhowtoremovespaces
Just Chill……… 🙂
- [Django]-How would you create a 'manual' django migration?
- [Django]-Get objects from a many to many field
- [Django]-ValueError: Related model u'app.model' cannot be resolved
Source:stackexchange.com