Python LOB to List - python

Using:
cur.execute(SQL)
response= cur.fetchall() //response is a LOB object
names = response[0][0].read()
i have following SQL response as String names:
'Mike':'Mike'
'John':'John'
'Mike/B':'Mike/B'
As you can see it comes formatted. It is actualy formatted like:\\'Mike\\':\\'Mike\\'\n\\'John\\'... and so on
in order to check if for example Mike is inside list at least one time (i don't care how many times but at least one time)
I would like to have something like that:
l = ['Mike', 'Mike', 'John', 'John', 'Mike/B', 'Mike/B'],
so i could simply iterate over the list and ask
for name in l:
'Mike' == name:
do something
Any Ideas how i could do that?
Many thanks
Edit:
When i do:
list = names.split()
I receive the list which is nearly how i want it, but the elements inside look still like this!!!:
list = ['\\'Mike\\':\\'Mike\\", ...]

names = ['\\'Mike\\':\\'Mike\\", ...]
for name in names:
if "Mike" in name:
print "Mike is here"
The \\' business is caused by mysql escaping the '
if you have a list of names try this:
my_names = ["Tom", "Dick", "Harry"]
names = ['\\'Mike\\':\\'Mike\\", ...]
for name in names:
for my_name in my_names:
if myname in name:
print myname, " is here"

import re
pattern = re.compile(r"[\n\\:']+")
list_of_names = pattern.split(names)
# ['', 'Mike', 'Mike', 'John', 'John', 'Mike/B', '']
# Quick-tip: Try not to name a list with "list" as "list" is a built-in
You can keep your results this way or do a final cleanup to remove empty strings
clean_list = list(filter(lambda x: x!='', list_of_names))

Related

how to rearrange a split for a input

I have a task to rearrange a sentence with the first name middle initial and last name to last first middle. I don't know how to do this without a stated search or how to make a index. Ive tried it with finding a space between but I don't know how I would rearrange doing this. This is what I have but it doesn't work. If I could get help defining each part I think I could do the rest on my own.
name = input("Enter a name like 'First I. Last: ")
index = name.find(" ")
rearranged = name[3:index]
If you're on Python 3.8+, you should be able to use it like this:
name = 'John C. Smith'
rearranged_name = ' '.join([(word := name.split(' '))[2], *word[:2]])
print(rearranged_name)
Note that in the above, you could also write out *word[:2] like word[0], word[1] if you prefer to be more explicit.
Output:
Smith John C.
If you're using an earlier version than 3.8, the following alternative should still work for your use case (adapted from a comment by #TheLazyScripter).
*rest, last = name.split(' ')
rearranged_name = ' '.join([last, *rest])
Where *rest can also be reworded as two separate variables - first, mi.
If you expect an exact number of fields, you could assign them directly to names
first, middle_i, last = input("Enter a name like 'First I. Last': ").split()
However, you'll probably find it better/easier to use a regular expression to let the fields be optional or write some other custom parser
import re
name = input("Enter a name like 'First I. Last': ")
match = re.match(r"(?P<first>\w+) (?:(?P<middle>\w\.) )?(?P<last>\w+)", name)
if not match:
raise ValueError(f"name is invalid: {name}")
# match.groupdict() is a dict with the members named in it now
>>> name = input("Enter a name like 'First I. Last': ")
Enter a name like 'First I. Last': a b. c
>>> name
'a b. c'
>>> match = re.match(r"(?P<first>\w+) (?:(?P<middle>\w\.) )?(?P<last>\w+)", name)
>>> match.groupdict()
{'first': 'a', 'middle': 'b.', 'last': 'c'}
...
>>> name = "foo bar"
...
>>> match.groupdict()
{'first': 'foo', 'middle': None, 'last': 'bar'}

comprehension list in pymongo query

Good morning everyone,
I would like to write a pymongo snippet to query from a database all documents having a specific field value within a given list (so also a value which is a subset of any element in the given list).
Within python, I would have two lists and I would like to find all elements from list one which are contained in at least one element from list two. Eg:
list1 = ['abc', 'bob', 'joe_123']
list2 = ['abc', 'joe', 'mike']
for string in list2:
print( string, any([ string in xxx for xxx in list1 ]) )
which gives the correct result:
abc True
joe True
mike False
How could I get the same from pymongo? I tried the "$in" operator, but the result is not complete.
from pymongo import MongoClient
from pprint import pprint
client = MongoClient('mongodb://localhost:27017')
db = client['test_query']
my_coll = db['test_collection']
list1 = ['abc', 'bob', 'joe_123']
list2 = ['abc', 'joe', 'mike']
for string in list2:
my_coll.insert_one({'string' : string})
cursor = my_coll.find({'string' : { '$in' : list1} })
which misses the case in joe < joe_123
pprint([c for c in cursor])
[{'_id': ObjectId('5f60ce9b682ff1bf4dcafb94'), 'string': 'abc'}]
Could anyone give me a hint on this?
More generally, what is the syntax to incorporate a python comprehension list into a pymongo query?
Thank you so much
Marco
$in in mongodb does not match on a partial string.
To do this you must use $regex, e.g.
for string in list1:
my_coll.insert_one({'string' : string})
query = {'$regex': '|'.join(list2)}
cursor = my_coll.find({'string' : query})
pprint([c for c in cursor])

Checking the elements of a list for multiple strings

Say I have a list:
['[name]\n', 'first_name,jane\n', 'middle_name,anna\n', 'last_name,doe\n', '[age]\n', 'age,30\n', 'dob,1/1/1988\n']
How could I check if the strings 'jane', 'anna' and 'doe' are ALL contained in an element of the list.
For each name you can use any to see if it is contained in any of the strings in the list, then make sure this is true for all of the names
>>> data = ['[name]\n', 'first_name,jane\n', 'middle_name,anna\n', 'last_name,doe\n', '[age]\n', 'age,30\n', 'dob,1/1/1988\n']
>>> names = ['jane', 'anna', 'doe']
>>> all(any(name in sub for sub in data) for name in names)
True

How can I extract names from a concatenated string using Python?

Suppose I have a string of concatenated names like so:
name.s = 'johnwilliamsfrankbrown'.
How do I go from here to a list of names and surnames ["john", "williams", "frank", "brown"]?
So far I only found pieces of code to extract words from non concatenated strings.
As timgeb noted in the comments, this is only possible if you already know which names you expect. Assuming that you have this information, you can extract them like this:
>>> import re
>>> names = ['john', 'frank', 'brown', 'williams']
>>> regex = '(' + '|'.join(names) + ')'
>>> separated_names = re.findall(regex, 'johnwilliamsfrankbrown')
>>> separated_names
['john', 'williams', 'frank', 'brown']

Removing characters from a tuple

Im using
Users = win32net.NetGroupGetUsers(IP,'none',0),
to get all the local users on a system. The output is a tuple,
(([{'name': u'Administrator'}, {'name': u'Guest'}, {'name': u'Tom'}], 3, 0),)
I want to clean this up so it just prints out "Administrator, Guest, Tom". I tried using strip and replace but you cant use those on tuples. Is there a way to convert this into a string so i can manipulate it or is there an even simpler way to go about it?
This should not end with a comma:
Users = win32net.NetGroupGetUsers(IP,'none',0),
The trailing comma turns the result into a single item tuple containing the result, which is itself a tuple.
The data you want is in Users[0].
>>> print Users[0]
[{'name': u'Administrator'}, {'name': u'Guest'}, {'name': u'Tom'}]
To unpack this list of dictionaries we use a generator expression:
Users = win32net.NetGroupGetUsers(IP,'none',0)
print ', '.join(d['name'] for d in Users[0])
', '.join(user['name'] for user in Users[0][0])
input = (([{'name': u'Administrator'}, {'name': u'Guest'}, {'name': u'Tom'}], 3, 0),)
in_list = input[0][0]
names = [x['name'] for x in in_list]
print names
[u'Administrator', u'Guest', u'Tom']

Categories

Resources