Printing sql query results - python-3.x

Two separate but related questions to querying a database and sending the results to discord:
Example code:
async def query(ctx):
try:
cursor.execute("SELECT record.name, COUNT(place.name) .....")
for row in cursor:
await ctx.send(row)
except Error as e:
print(e)
Each row prints as it's own message and displays in the following format:
('potato', 1)
('orange', 1)
('steak', 1)
I would like to append all the rows into a single message as the results are numerous enough that this blows the channel up with spam messages. Each row on it's own line and part 2, remove the query result formatting.
It should look like this(parentheses indicating one message not visible formatting):
"
potato 1
orange 2
steak 1
"
I have tried to change "await ctx.send(row)" to "await ctx.send(row[0],row[1]) to remove the formatting but I know send doesn't operate in the same manner as print() and it's instead looking for a string.
"await ctx.send(row[0].join(row[1]))" fails because: "TypeError: can only join an iterable"
Further I have no clue how to get the result from cursor append it into a single string with line breaks.
Thanks in advance for any advice.

This is just basic python
message = ""
for veg, i in cursor.fetchall(): # Or however you named it
message += f"{veg} {i}\n"
The message var now looks like this:
"potato 1
orange 2
steak 1"

Related

Incorrect number of bindings supplied. The current statement uses 10, and there are 11 supplied

import sqlite3 ,csv
with open("BollywoodMovieDetail.csv","r")as file:
no_records = 0
for row in file:
c.execute("INSERT INTO Movie VALUES(?,?,?,?,?,?,?,?,?,?)", row.split(","))
conn.commit()
no_records += 1
conn.close()
print('\n{} Records Transferred'.format(no_records))
i dont know why the code is terminating my guess is that their is cell which have "," in it amd cousing problem. If it is so please let me know how to fix it. I m new to SQLITE3 so its getting hectic
Your insert statement expects 10 values per record. The error message implies that one or more lines has 11 values after splitting by comma, or, alternatively, one or more lines have 10 commas. You may iterate over your file and try to find the offending line(s):
with open("BollywoodMovieDetail.csv", "r") as file:
for row in file:
parts = row.split(",")
if len(parts) == 11:
print(row)

Ingesting data into spark using socketTextStream

I am fetching tweets from the twitter API and then forwarding them through a tcp connection into a socket where spark is reading data from. This is my code
For reference line will look something like this
{
data : {
text: "some tweet",
id: some number
}
matching_rules: [{tag: "some string", id: same number}, {tag:...}]
}
def ingest_into_spark(tcp_conn, stream):
for line in stream.iter_lines():
if not (line is None):
try :
# print(line)
tweet = json.loads(line)['matching_rules'][0]['tag']
# tweet = json.loads(line)['data']['text']
print(tweet, type(tweet), len(tweet))
tcp_conn.sendall(tweet.encode('utf-8'))
except Exception as e:
print("Exception in ingesting data: ", e)
the spark side code:
print(f"Connecting to {SPARK_IP}:{SPARK_PORT}...")
input_stream = streaming_context.socketTextStream(SPARK_IP, int(SPARK_PORT))
print(f"Connected to {SPARK_IP}:{SPARK_PORT}")
tags = input_stream.flatMap(lambda tags: tags.strip().split())
mapped_hashtags = tags.map(lambda hashtag: (hashtag, 1))
counts=mapped_hashtags.reduceByKey(lambda a, b: a+b)
counts.pprint()
spark is not reading the data sent over the stream no matter what I do. But when I replace the line tweet = json.loads(line)['matching_rules'][0]['tag'] with the line tweet = json.loads(line)['data']['text'] it suddenly works as expected. I have tried printing the content of tweet and its type in both lines and its string in both. Only difference is the first one has the actual tweets while second only has 1 word.
I have tried with many different types of inputs and hard-coding the input as well. But I cannot imagine why reading a different field of a json make my code to stop working.
Replacing either the client or the server with netcat shows that the data is being sent over the socket as expected in both cases
If there are no solutions to this I would be open to knowing about alternate ways of ingesting data into spark as well which could be used in this scenario
As described in the documentation, records (lines) in text streams are delimited by new lines (\n). Unlike print(), sendall() is a byte-oriented function and it does not automatically add a new line. No matter how many tags you send with it, Spark will just keep on reading everything as a single record, waiting for the delimiter to appear. When you send the tweet text instead, it works because some tweets do contain line breaks.
Try the following and see if it makes it work:
tcp_conn.sendall((tweet + '\n').encode('utf-8'))

checking a word in the text and print() a command once

Using these commands I get the three sentences.
AnyText = driver.find_elements_by_xpath('AnyXpath')
for AnyText1 in AnyText:
print(AnyText1.text)
In the console, I get something like that:
**
1) Hello my name is John
**
2) Hello my name is Mark
**
3) Hello my name is Alex..
How can I check that all three sentences have the word "name"
and print("OK") if the word is in the sentence (element) and print("ERROR") if not.
Im try:
AnyText = driver.find_elements_by_xpath('AnyXpath')
Text = 'name'
if all(Text in AnyText1 for two in AnyText1):
print('OK')
else:
print('ERROR')
but this method only checks the first element (first sentence). I also tried something like this
AnyText = driver.find_elements_by_xpath('AnyXpath')
Text = 'name'
for AnyText1 in AnyText:
if all(Text in AnyText1):
print('OK')
else:
print('ERROR')
but I get many times OK or ERROR
UPD:
With a question on the text, I figured out with your help. Now I want to understand the numbers)
I have a loop that checks the next number more or less. If more, writes ERROR, if less, writes OK
sort_month=driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[1]/div/div[13]/table/thead/tr/th[3]/a[4]').click()
month2=driver.find_element_by_xpath('//*[starts-with(#id, "td_")]/td[3]/span[3]')
month2=month2.text.replace("'", "").replace(" ", "")
buffer = 0
if int(month2) > buffer:
print()
buffer = int(month2)
month1=driver.find_elements_by_xpath('//*[starts-with(#id, "td_")]/td[3]/span[3]')
for spisok_month in month1:
spisok_month = spisok_month.text.replace("'", "").replace(" ", "")
if int(spisok_month) > buffer:
print('ERROR')
elif int(spisok_month) < buffer:
print('OK')
else:
print('==')
buffer = int(spisok_month)
here I would also like to see OK or ERROR only once.
Any ideas?
The problem seems to be with the short form for loop in your first snippet. Basically it should look like the below:
AnyText = driver.find_elements_by_xpath('AnyXpath')
Text = 'name'
if all(Text in AnyText1.text for AnyText1 in AnyText):
print('OK')
else:
print('ERROR')
UPDATE:
On the updated part of your question, this is a different implementation as you have to update the condition in each iteration. For readability, it probably makes sense to keep this expanded:
outcome = 'OK'
for spisok_month in month1:
spisok_month = spisok_month.text.replace("'", "").replace(" ", "")
if int(spisok_month) > buffer:
outcome = 'ERROR'
elif outcome == 'OK' and int(spisok_month) == buffer:
outcome = '=='
buffer = int(spisok_month)
print(outcome)
Note: The update is almost a separate question. this means that either your first question was not representative of the actual problem or you should ask in a separate post
In you code AnyText1 is a WebElement, not a text. You should use AnyText1.text to get text and then it will work:
AnyText = driver.find_elements_by_xpath('AnyXpath')
Text = 'name'
# AnyText1 is a WebElement and you should get text
if all(Text in AnyText1.text for AnyText1 in AnyText):
print('OK')
else:
print('ERROR')
Please check coding conventions to improve code style.

Can't print an integer that has been converted to a string

I am trying to do something that should be very simple. I am a bit frustrated for why my code won't work, so any help is appreciated :).
My program reads in data, and then makes a dictionary that records the number of times a particular item occurs (I am using Twitter data and counting hashtag occurrences). I want to output the top tweets, and have found a nice easy way to do that using the following:
def main():
tweet_file = open(sys.argv[1])
tweet_dic = lines(tweet_file) #function that makes my dictionary
for i in range(0,10):
big_key = max(tweet_dic, key = lambda i: tweet_dic[i])
big_value = tweet_dic[big_key]
sys.stdout = big_key + " " + str(big_value)
del tweet_dic["big_key"]
tweet_file.close()
The error I get on using this is AttributeError: 'str' object has no attribute 'write'
Now I have outputted the two different values into terminal using print just fine, they can be put in two different print statements with no problems since I don't have to concatenate or anything. I have checked the two variables types, and as expected they are always str & int.
My understanding of the str(integer to convert) function is that you should be able to pass in an integer and get a string representation back! After it has been converted, I have been able to print out things like this in the past with no issues.
Things to consider that may be throwing it out - the big_key can sometimes be a string that was converted from Unicode by .encode('utf-8'). Otherwise the output from my file (printing on separate lines) looks like:
MTVHottest 60
KCAMexico 38
EXO 26
CD9 24
Unicode 19
Coders 18
AlonsoVillapandoTrendy 17
Unicode 14
Unicode 14
Any ideas? Thanks in advance!
The error you're getting is because of this: https://docs.python.org/2/library/sys.html#sys.stdout
stdout and stderr needn’t be built-in file objects: any object is acceptable as long as it has a write() method that takes a string argument.
stdout and stderr are the locations where the data is written, you aren't supposed to assign data itself to stdout, which is what it looks like you're doing.
As others have mentioned you would assign stdout to where you want and then use the print command to actually get the data to go there. But if all you're trying to do is print a line of text to a file then why not just open the file and write to it normally?
with open(tweet_file, 'w') as f:
f.write(big_key + " " + str(big_value))
The specific error you're getting is from the line:
sys.stdout = big_key + " " + str(big_value)
This is not how things are output to stdout.
Using a print would be the appropriate way to do that:
print(big_key + " " + str(big_value))
There are some other strange things in your code example, like for example using big_key and then "big_key" in quotes. So probably that's not the end of your bugs and errors.

if string not in line

I have a list with a bunch of lines (created from a 'show cdp neighbors detail
from a Cisco switch or router).
I want to perform some processing but only on lines that contain one of the following four strings:
important = (
"show cdp neighbors detail",
"Device ID: ",
"IP address: ",
"Platform: "
)
Right now my iterations work but I am processing over a large number of lines that end up doing nothing. I simply want to skip over the unimportant lines and do my processing over the four lines lines that contain the data that I need.
What I want to do (in English) is:
for line in cdp_data_line:
if any of the lines in 'cdp_data_line' does not contain one of the four strings in 'important', then continue (skip over the line).
After skipping the unwanted lines, then I can test to see which of the four lines I have left and process accordingly:
if all of the four strings in 'important' is not found in any line of cdp_data_line, the continue
if any of those four strings is found in a line,then process:
elif 'show cdp neighbors detail' in line:
do something
elif 'Device ID: ' in line:
do something
elif 'IP address: ' in line:
do something
elif 'Platform: ' in line:
do something
else:
sys.exit("Invalid prompt for local hostname - exiting")
I am trying to do it like this:
if any(s in line for line in cdp_data_line for s not in important):
continue
PyCharm says it doesn't know what 's' is, even though I found code snippets
using this technique that work in PyCharm. Not sure why mine is failing.
There has got to be a succinct way of saying: If string1 or string 2 or string 3 or string 4 is not in cdp_data_line, then skip (continue), otherwise
if I find any of those four strings, do something.
Any help would be greatly appreciated.

Resources