Using cx_Oracle converting query output to dictonary object - python

When using the cx_oracle module in python , when i run a query and print the output, it gives it as a tuple object. However I want to have the output as a dictionary object having it neatly :
{,'' , ,'' , ,'' , }
is there a quick thing I can do for this ?

I figured out a method to do this. Below is the relevant code snippet:
column_name = [column1,column2,column3]
for row in cur:
list_row = list(row) # convert the tuple row to a list type.
final = zip(column_names,list_row) # zip the two lists.
dict_final = dict(final) # Convert the resulting final list to a dictionary.
I don't know if there is a better way to do this, but this is what I came up with.

Related

Formatting output of floats in a dictionary

Im currently running some correlation in test in python. I get the results I need but they're in x.01234567890 and I'm trying to format them to output them into percentage.
this is reading from a csv.
results = (
{
clean_df['NZDUSD_Close'].corr(clean_df['USDJPY_Close']),
clean_df['NZDUSD_Close'].corr(clean_df['EURGBP_Close']),
....
eclean_df['AUDUSD_Close'].corr(clean_df['EURUSD_Close']),
clean_df['GBPUSD_Close'].corr(clean_df['EURUSD_Close'])
}
)
print(results)
The above works. There are 15 results and returns all in floats, but I get string errors when I formatted this as a tuple I decided to make it a dictionary.
I've tried the following for formatting the output:
print (f"{results:.2%}")
This works for a single variable but not the list.
for key in results.keys():
print(key, "{:.2f}".format(results[key]))
If the error your encountering is:
"AttributeError: 'set' object has no attribute 'keys'"
Then it's because your dictionary is inside a tuple and a tuple has no "keys" attribute. You would be best off if you used a tuple:
results = (...)
and printing it like:
print(f"{results:.2%}")
Doesn't work because python doesn't know to iterate over all the objects in the result.
Another way of printing the tuple is:
for val in results:
print("{:.2%}".format(val))
This will iterate over every value in results and print it in a 2 point percentage format

Python sqllite3 doesn't return list

I have a list of values (only one value for now)
testList = ["value1"] #or is this an array in python??
and I would like to get this list from database
so I tried like this (assuming I have myTerribleTable in my DB and it has one atribute named "myAtribute" which has values "value1"
conn = sqlite3.connect("mydb.db")
so I execute the query: print(conn.execute(SELECT myAtribute FROM myTerribleTable).fetchall())
so I expect to get back ['value1']
but I get [('value1',)] this is not a list, it looks like a tuple even prepending evething with list like that print(list(conn.execute(SELECT myAtribute FROM myTerribleTable).fetchall())) doesn't work I still get [('value1',)] instead of desired ['value1']
not sure what is happening
Thanks for Anwsering and Best Regards
rows = conn.execute("SELECT myAttribute FROM myTerribleTable").fetchall()
output = List()
for row in rows:
output.append(row.["myAttribute"])
With this loop you could convert the row objects into a list of values
print(list(conn.execute(SELECT myAtribute FROM myTerribleTable).fetchall()[0]))
How about this?

How to store list in rows of mysql using Python/Flask?

I am getting some values from a html form and I am storing these values to a list. List is like:
["string1", "string2", "string3", "string4", "string5"]
I want to store these values in rows of mysql but I am confused how to do?
What I did till now is:
descrip = []
descrip.append(description1)
descrip.append(description2)
descrip.append(description3)
descrip.append(description4)
descrip.append(description5)
for r in descrp:
result_descrp = db.execute("""INSERT INTO description(id,description) VALUES (1,%s)""",((descrip))
return render_template('forms/success.html')
But I am getting this error:
TypeError: not all arguments converted during string formatting
At first, You use the placeholder %s in the format string which expect a str. But you pass a list to it.
And I don't know the type of description in your schema. If you just want to save the string presentation of list in the database, you can transform list to str with str(desciption).
And Mysql also support json type of field.(MariaDB also support json type.)
descrip = []
descrip.append(description1)
descrip.append(description2)
descrip.append(description3)
descrip.append(description4)
descrip.append(description5)
for r in range(5):
if descrip[r]:
result_add_event = db.execute("""INSERT INTO event_description(event_id,title,description, created_at) VALUES (%s,%s,%s)""",(id,descrip[r],timestamp))
This above code worked very fine. :)
Special thanks to #shiva and also to those who helped me.

Converting a dataframe into JSON (in pyspark) and then selecting desired fields

I'm new to Spark. I have a dataframe that contains the results of some analysis. I converted that dataframe into JSON so I could display it in a Flask App:
results = result.toJSON().collect()
An example entry in my json file is below. I then tried to run a for loop in order to get specific results:
{"userId":"1","systemId":"30","title":"interest"}
for i in results:
print i["userId"]
This doesn't work at all and I get errors such as: Python (json) : TypeError: expected string or buffer
I used json.dumps and json.loads and still nothing - I keep on getting errors such as string indices must be integers, as well as the above error.
I then tried this:
print i[0]
This gave me the first character in the json "{" instead of the first line. I don't really know what to do, can anyone tell me where I'm going wrong?
Many Thanks.
If the result of result.toJSON().collect() is a JSON encoded string, then you would use json.loads() to convert it to a dict. The issue you're running into is that when you iterate a dict with a for loop, you're given the keys of the dict. In your for loop, you're treating the key as if it's a dict, when in fact it is just a string. Try this:
# toJSON() turns each row of the DataFrame into a JSON string
# calling first() on the result will fetch the first row.
results = json.loads(result.toJSON().first())
for key in results:
print results[key]
# To decode the entire DataFrame iterate over the result
# of toJSON()
def print_rows(row):
data = json.loads(row)
for key in data:
print "{key}:{value}".format(key=key, value=data[key])
results = result.toJSON()
results.foreach(print_rows)
EDIT: The issue is that collect returns a list, not a dict. I've updated the code. Always read the docs.
collect() Return a list that contains all of the elements in this RDD.
Note This method should only be used if the resulting array is
expected to be small, as all the data is loaded into the driver’s
memory.
EDIT2: I can't emphasize enough, always read the docs.
EDIT3: Look here.
import json
>>> df = sqlContext.read.table("n1")
>>> df.show()
+-----+-------+----+---------------+-------+----+
| c1| c2| c3| c4| c5| c6|
+-----+-------+----+---------------+-------+----+
|00001|Content| 1|Content-article| |2018|
|00002|Content|null|Content-article|Content|2015|
+-----+-------+----+---------------+-------+----+
>>> results = df.toJSON().map(lambda j: json.loads(j)).collect()
>>> for i in results: print i["c1"], i["c6"]
...
00001 2018
00002 2015
Here is what worked for me:
df_json = df.toJSON()
for row in df_json.collect():
#json string
print(row)
#json object
line = json.loads(row)
print(line[some_key])
Keep in mind that using .collect() is not advisable, since it collects the distributed data frames, and defeats the purpose of using data frames.
To get an array of python dicts:
results = df.toJSON().map(json.loads).collect()
To get an array of JSON strings:
results = df.toJSON().collect()
To get a JSON string (i.e. a JSON string of an array):
results = df.toPandas().to_json(orient='records')
and using that to get an array of Python dicts:
results = json.loads(df.toPandas().to_json(orient='records'))

Converting tuple into list does not work

I query my SQlite Database with a loop to retrieve data from it.
connector = sqlite3.connect("somedb.db")
selecter = connector.cursor()
selecter.execute(''' SELECT somedata FROM somedb''')
for row in selecter:
l = list(row)
print (l)
print (type(l))
Then I try do use formatting to append the retrieved data to something else
this = detect.that( "{}", pick, summary).format(l)
But it comes back with this:
AttributeError: 'tuple' object has no attribute 'format'
I also tried this
s = " ".join(str(row) for row in selecter)
for the l = list(row) statement but it comes back with the same errormessage and it seems that it converts all my 50 separate selections into one string what I dont want.
However, when I run this
print (type(l))
or
print (type(s))
it returns me list or stringas a type. So the converting worked, but the .format does not take it because it thinks it is a tuple.
How comes?
Change your detect.that line to this:
this = str(detect.that("{}", pick, summary)).format(1)

Categories