I have a string
"Tyson Invitational 02/08/2013','#FFFFCC')""; ONMOUSEOUT=""kill()"" >6.54"
How would I use regex to select everything after the right-pointing bracket? Aka how would I get the 6.54?
I've tried
\>(.*)
but I'm not sure it's working properly. I use
m = re.search( '\>(.*)', row_out[5])
and get
<_sre.SRE_Match object at 0x10b6105d0>
Not sure what the issue is.
Thanks!
>>> import re
>>> str="""Tyson Invitational 02/08/2013','#FFFFCC')""; ONMOUSEOUT=""kill()"" >6.54"""
>>> re.search('\>(.*)',str)
<_sre.SRE_Match object at 0x7f0de9575558>
same as you got before. However assign the search result to a variable and
>>> f=re.search('\>(.*)',str)
>>> f.groups()
('6.54',)
Related
I am self-learning, and not sure if this is a duplicate questions. In the example below:
Why does first list(a)==a print nothing?
Also why does a become false? And then turning back to a range object?
>>> a=range(10,17)
>>> list(a)==a
>>> list(a)==a
False
>>> a
False
>>> a
range(10, 17)
>>>
Moreover, if I call A once, then call list(A)==A. It always gives range(10, 17). Could anyone also explain this?
>>> A=range(10,17)
>>> A
>>> list(A)==A
range(10, 17)
>>>
Sorry for the rookie questions, I must miss some very basic logic here. Thank you for any ideas!
I would like to use the function substr(my_var,1,2) of SAS in Python. I tried a lot of functions like contains(), split() but it didn't work.
The functions contains() and split() work only for a string value. I would like to use it on a Python Series without using a for.
Thanks a lot for your help
A string in python can be sliced like any list:
>>> str = 'Hello World'
>>> str[1:3]
'el'
>>> str[1:-2]
'ello Wor'
To get substrings for multiple strings, you can use list comprehensions:
>>> strs = ['Hello World', 'Foobar']
>>> [ str[1:4] for str in strs]
['ell', 'oob']
In python, you may try this:
my_var[1:3]
This gets sub string of my_var from position 1 to 3 (exclusive).
I am planning to do a semester project on English alphabet recognition using Python. I am using Python for the first time. What are the tools and methodologies needed? Please help.
Your question is not clear and I suggest you rephrase it or risk having it put on hold.
However, if you mean checking whether a character in a string is a letter, you can use the built-in isaplha() method.
Examples:
>>> 'a'.isalpha()
True
>>> 'x'.isalpha()
True
>>> my_char = 'd'
>>> my_char.isalpha()
True
>>> '1'.isalpha()
False
>>> my_var = '#'
>>> my_var.isalpha()
False
I am trying to find out if there are better faster ways to clean this returned string. Or is this the best way. It works, but more efficient ways are always wanted.
I have a function that returns the following output:
"("This is your:, House")"
I clean it up before printing with:
a = re.sub(r'^\(|\)|\,|\'', '', a)
print a
>>> This is your: House
I also learn a lot from the different ways people do things.
You don't need to use regular expression to do this.
>>> import string
>>> a = '"("This is your:, House")"'
>>> ''.join(x for x in a if x not in string.punctuation)
'This is your House'
>>> tbl = string.maketrans('', '')
>>> a.translate(tbl, string.punctuation)
'This is your House'
s='"("This is your:, House")"'
s.replace('\"','').replace('(','').replace(')','').replace(',','').replace(':','')
'This is your House'
I want to remove 'blog/' substring from slug field of multiple objects according to this and this docs:
>>> import re
>>> from django.db.models import F
>>> p = re.compile('blog/')
>>> Blog.objects.update(slug=p.sub('', F('slug')))
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: expected string or buffer
I tried to add str() to the last string, and it passes without errors:
>>> Blog.objects.update(slug=p.sub('', str(F('slug'))))
but it inserts (DEFAULT: ) into slug field for all objects.
Any suggestions?
To update multiple objects at once in Django with regular expressions over a queryset you can use a Func expression to access regex functions in your database:
django.db.models import F, Func, Value
pattern = Value(r'blog(/?)') # the regex
replacement = Value(r'new-blog-slug\1') # replacement string
flags = Value('g') # regex flags
Blog.objects.update(
slug=Func(
models.F('slug'),
pattern, replacement, flags,
function='REGEXP_REPLACE',
output_field=models.TextField(),
)
)
Check your DB vendor documentation for details and specific functions support.
Use raw strings r'' in pattern and replacement to avoid having to escape the backslashes.
Reference matched substrings in replacement using \n with n from 1 to 9.
You can use F expressions to provide pattern, replacement and flags from fields of each instance:
pattern = F('pattern_field')
replacement = F('replacement_field')
flags = F('flags_field')
You can also use the Func expression to make annotations.
Currently there is an open pull request to add regular expressions database functions in Django. Once merged you will probably have RegexpReplace, RegexpStrIndex and RegexpSubstr function expressions available under django.db.models.functions to make your code more concise and have a single API unified across DB vendors.
You can't do that. The update is done completely within the database, so it must be something translatable to SQL, which your code isn't. You'll need to iterate through and update:
for blog in Blog.objects.filter(slug__startswith='blog/'):
blog.slug = blog.slug.replace('blog/', '')
blog.save()
A little late but for those who need a solution today
Note:New in Django 2.1.
class Replace
Usage example from Documentation:
>>> from django.db.models import Value
>>> from django.db.models.functions import Replace
>>> Author.objects.create(name='Margaret Johnson')
>>> Author.objects.create(name='Margaret Smith')
>>> Author.objects.update(name=Replace('name', Value('Margaret'), Value('Margareth')))
2
>>> Author.objects.values('name')
<QuerySet [{'name': 'Margareth Johnson'}, {'name': 'Margareth Smith'}]>