Python SQLAlchemy response type to string - python

When I run a SQLAlchemy query to get values from a SQLAlchemy.String() column;
query = db.session.query(self.hosts_class.cluster.distinct())
for row in query.all()
Iterating and printing the result object results in output like;
('sometext',)
If I browse the row object I can see that row contains a key, value pair of "0": "sometext". What's the correct way to access this value from row?

row is a single-element tuple, so you could access by index:
for row in query:
print(row[0])
or by unpacking the tuple
for row, in query: # <- note the comma
print(row)

Related

Python: shapefile.Reader, how to set up .iloc?

When I iterate over dataframe or geodataframe and I want to set up some section, I use df.iloc[0:100]. How can I set up some section when I use shapefile.Reader? For example 0-100 rows.
with shapefile.Reader('C:/Users/ja/Inne/Desktop/Praca/Orto_PL1992_piksel3-50cm/PL1992_5000_025') as shp:
total_rows = shp.numRecords
for row_num, row in enumerate(shp.iterRecords()):
print(row)
A generator is not subscriptable and iterRecords() returns a generator. Instead, use shapeRecords() (or records()). It gives you a list.
rows = shapefile.Reader(shapefile_path).shapeRecords()[0:100]
for row_num, row in enumerate(rows):
print(row_num, row)

How to get list using sql query in python?

I am defining a function here and making a query.
def fetch(temp_pass,temp_accno):
cur.execute('''SELECT id, name, acc_no, ph_no,address, email,balance
FROM accounts
WHERE id = %s and acc_no = %s''',
(str(temp_pass), str(temp_accno)));
row = cur.fetchall()
print(row[2])
In this row should be a list of length 7 but when I run print(row[2])
it gives me error that list index out of range.
This is the error I get
File "Accounting.py", line 13, in fetch
print(row[2])
IndexError: list index out of range
row = cur.fetchall() won't give you a row but a list of rows, so row is not a row at all and row[2] is the third row in the list, not the third field in a row. If you want only a row use cur.fetchone().
Note the the query might return several rows and it is not clear what you want to do in that case so I won't deal with it here. cur.fetchone() will give you only one row anyway.
row[2] returns the 3rd row of the list of rows. row[0][2] returns the 3rd column of the 1st row.
You could run a snippet like this to visualize what gets returned:
cur.execute(...)
for row in cur:
print(row)

Iterate through dataframe and select null values

I am trying to iterate through a dataframe that has null values for the column = [myCol]. I am able to iterate through the dataframe fine, however when I specify I only want to see null values I get an error.
End goal is that I want to force a value into the fields that are Null which is why I am iterating to identify which are first.
for index,row in df.iterrows():
if(row['myCol'].isnull()):
print('true')
AttributeError: 'str' object has no attribute 'isnull'
I tried specifying the column = 'None' since that is the value I see when I print the iteration of the dataframe. Still no luck:
for index,row in df.iterrows():
if(row['myCol']=='None'):
print('true')
No returned rows
Any help greatly appreciated!
You can use pd.isnull() to check if a value is null or not:
for index, row in df.iterrows():
if(pd.isnull(row['myCol'])):
print('true')
But seems like you need df.fillna(myValue) where myValue is the value you want to force into fields that are NULL. And also to check the NULL fields in a data frame you can invoke df.myCol.isnull() instead of looping through rows and check individually.
If the columns are of string type, you might also want check if it is empty string:
for index, row in df.iterrows():
if(row['myCol'] == ""):
print('true')

cursor.fetchall() in Python

After saving some data in a variable with cursor.fetchall(), it looks as follows:
mylist = [('abc1',), ('abc2',)] this is apparently a list.
That is not the issue.
The problem is that the following doesn't work:
if 'abc1' in mylist
it can't find 'abc1'. Is there a way in Python to do it easily or do I have to use a loop?
fetchall() returns a row list, i.e., a list containing rows.
Each row is a tuple containing column values. There is a tuple even when there is only one column.
To check whether a row is in the row list, you have to check for the row instead of the column value alone:
if ('abc1',) in mylist
This is problem with using select * statement.
Instead use select col1,col2 from table_name
Below code might help
sql = "select col1,col2,col3 from table_name"
cursor.execute(sql) # initialize cursor in your way
input_dict = dict( (row[0],(row[1],row[2])) for row in cursor.fetchall() )

Sqlite query to python dictionary

I have searched the docs and SO and could not find anything to resolve my issue. I am trying to call a select from my sqlite database and add it to a dictionary with the columns as keys. When I do this it returns a row for each column/key. It is has 14 columns and if I only have 4 rows it repeats for each one. This was the first attempt
columns = [desc[0] for desc in cursor.description]
results = []
for row in r:
Summary = {}
items = zip(columns, row)
for (items, values) in items:
Summary[items] = row
results.append(Summary)
Then I also tried the row_factory as given in the docs. That didn't work. My end goal is to be able to print out to a text file verticly by using
for x in results:
print x[name]
print x[email]
etc
Any help is appreciated
You are creating your dictionary incorrectly. Use:
for row in r:
summary = dict(zip(columns, row))
results.append(summary)
instead.
Your code sets the whole row sequence as the value for each key in Summary, instead of the individual column value, then appending that same dictionary to the results list for each column key..

Categories