I'm trying to use a Class (adns-python) which is expecting a list in the format:
domain_names = ["google.com", "yahoo.com"]
This is working when I declare the list that way manually. However, I'm trying to use a list returned from mysql using python-mysqldb.
When I look at what is being returned from mysql using:
type(mysql_rows)
This also shows as a list, but when view the result:
print(mysql_rows)
I can see the list is in the format:
[('google.com',), ('yahoo.com',)]
I've tried forcing the output to a list again using list(mysql_rows) which didn't work. I've tried parsing the text manually to make it look like the list using:
text_rows = "[" + ", ".join'"%s"' % i for i in mysql_rows = "]"
Which then shows as the correct format, but it is a string not a list so this doesn't work either.
This is my first few days learning python, so I'm sorry if this is an obvious/stupid question.
Thanks
The list is a list of tuples. A simple
lst = [x for x, in mysql_rows]
should be sufficient.
mysql returns a list of tuples. Each tuple is a result row in your result set. If you want a list of only the first "column" in the result, try this:
first_column = [x[0] for x in mysql_rows]
Related
I am new to this sort of stuff, so sorry if it's really simple and I am just being stupid.
So I have this variable with some bytes in it (not sure if that's the right name.)
data = b'red\x00XY\x001\x00168.93\x00859.07\x00'
I need to convert this to a list. The intended output would be something like.
["red","XY","1","169.93","859.07"]
How would I go about doing this?
Thank you for your help.
We can use the following line:
[x.decode("utf8") for x in data.split(b"\x00") if len(x)]
Going part by part:
x.decode("utf8"): x will be a bytes string, so we need to convert it into a string via `.decode("utf8").
for x in data.split(b"\x00"): We can use python's built in bytes.split method in order to split the byte string by the nullbytes to get an array of individual strings.
if len(x): This is equivalent to if len(x) > 0, since we want to discard the empty string at the end.
This code may help you to understand if you want exact same output using the pop() function.
data = 'red/x00XY/x001/x00168.93/x00859.07/x00' # I change "/" mark from "\" because i'm using Linux otherwise it will give error in Linux
new_list = [] # There is a variable that contain empty list
for item in data.split('/x00'): # Here I use split function by default it splits variable where "," appears but in this case
new_list.append(item) # you need list should be separated by "/" so that's why I gave split('/x00') and one by list appended
print(new_list)
The folowing expression works fine, returning the values that match the values list:
[...].find({"FieldName":{"$in":["Value1", "Value2", "Value3"]}})
But i have the list of values in a list object, like this:
valuesList = list()
valuesList.append("Value1")
valuesList.append("Value2")
valuesList.append("Value3")
But using the list object, a get no results:
[...].find({"FieldName":{"$in":[valuesList]}})
I Have also tried expanding the list in a formated string, like this:
strList = ', '.join(valuesList)
[...].find({"FieldName":{"$in":[strList]}})
but also, no results.
Note: If i force the list to have only one value, it works. Only when multiple values are suplied, the result is blank.
Any ideas on how to use "$in" in pymongo with a list of values in a list object?
I believe your problem is the fact that you have a list inside of a list.
Instead of:
[...].find({"FieldName":{"$in":[valuesList]}})
Try:
[...].find({"FieldName":{"$in":valuesList}})
I am quite new to Python. I have a list containing some more lists, but only in two dimension (e.g. List[a][b]). Now for every value [a] I want to access a certain value [b] (10 in this case). For now it would be sufficent to just print every value List[a][10]. I tried:
for rec in List:
print List[rec][10]
That gives me the error "TypeError: list indices must be integers, not list". However if I just try "print List[0][10]" it gives me the value I want. In my for-Loop isn't rec an integer? How could I solve this problem?
Additional info: I am using Python 2.4.3 to be able to use the shapefile library that lets me access GIS data (my list).
for rec in List:
print rec[10]
should work.
You need to use:
for rec in List:
print rec[10]
or
for i range(len(List)):
print List[i][10]
rec in your case is not an integer, it is the first item of list. For you to use it as an integer you must add range in the for loop, like "for rec in range(0,len(List))"
Im pretty new to Python.
I have a list which looks like the following:
list = [('foo,bar,bash',)]
I grabbed it from and sql table (someone created the most rubbish sql table!), and I cant adjust it. This is literally the only format I can pull it in. I need to chop it up. I can't split it by index:
print list[0]
because that just literally gives me:
[('foo,bar,bash',)]
How can I split this up? I want to split it up and write it into another list.
Thank you.
list = [('foo,bar,bash',)] is a list which contains a tuple with 1 element. You should also use a different variable name instead of list because list is a python built in.
You can split that one element using split:
lst = [('foo,bar,bash',)]
print lst[0][0].split(',')
Output:
['foo', 'bar', 'bash']
If the tuple contains more than one element, you can loop through it:
lst = [('foo,bar,bash','1,2,3')]
for i in lst[0]:
print i.split(',')
I'm a python newbie, using the cx_Oracle python package to execute a sql query:
cursor.execute("select tablespace_name from user_tablespaces")
to retrieve a list from an oracle 11g database. The results look like this:
('SYSTEM',)
('SYSAUX',)
('UNDOTBS1',)
('TEMP',)
('USERS',)
I need to remove the single quotes and the comma from each entry and then put it into an array, and am attempting to use python slicing to do so:
tablespaceNames = []
for result in cursor:
tablespaceNames.append(result[2:-3])
however, this is just giving me empty strings in my array:
()
()
()
()
Is it a problem with the object I'm getting from the query result, or am I using python slicing wrong?
You are slicing the tuples that represent each retrieved row rather than the strings that are the first (and only) elements of those rows. Further, you don't need to "get rid of the quotes" - that's just the interpreter doing its best to represent the data structure.
Your database returns the equivalent of the structure below - a list of tuples. Since you only selected a single field, each tuple only contains one element.
data = [
('SYSTEM',),
('SYSAUX',),
('UNDOTBS1',),
('TEMP',),
('USERS',)
]
So first let's extract those single elements to give ourselves a list of strings instead of a list of tuples.
sdata = [s[0] for s in data]
print(sdata)
The output you will see is
['SYSTEM', 'SYSAUX', 'UNDOTBS1', 'TEMP', 'USERS']
Then print out each of the strings in the tuple:
for s in sdata:
print(s)
The output from this code is
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
See - no quotes!
tableNameSpaces = [item [0][2:-3] for item in cursor]
The quotes will be in, though, since you're dealing with string literals.
Once you print them, however, the quotes will be gone.
As nicarus pointed out to me, probably you don't want to truncate your string literals at all, though I thought that's what you wanted.
In that case it would simply be:
tableNameSpaces = [item [0] for item in cursor]