how to signal EOF to psycopg2.copy_from()? - psycopg2

I'm trying to read from one (non-postgres) cursor and use the results to feed psycopg2.copy_from(). I seem to have everything working fine, EXCEPT the EOF condition. I have a wrapper for my cursor that turns it into a file-like object, and in that read() method I have:
row = self.readline()
if not row:
return ""
But this causes the copy_from(cursor_as_file, 'cm_outgoing') to choke with
ERROR: invalid input syntax for integer: ""
CONTEXT: COPY cm_outgoing, line 533, column id: ""
This kinda makes sense, as the first field in cm_outgoing is an integer, and passing a zero-length string. Should I be signaling EOF differently? Or am I missing something else?

The error was in a different place. At another point, I had a double-newline in the file. I'm not sure why copy_from() didn't complain there, but fixing the double-newline seems to have taken care of this problem.

Related

not getting return value through function or method in python

in this program i am iterating the function and adding the result into the file it works fine, no issue whatsoever but when i am trying to take the value from the return of last call, it just return nothing even though the variable is not empty.because the else part only runs for a single time.
#this is an ipynb file so spacing means they are getting executed from different blocks
def intersection(pre,i=0,point=0,count=0,result=dt):
index=-1
prefer=[]
# print(i)
if(0<i):
url = "../data/result.csv"
result= pd.read_csv(url,names=["a","b","c","d","e"])
if(i<len(pre)):
for j in result[pre[i]]:
index=index+1
if(demand[pre[i]][1] >= j):
prefer.append(result.iloc[index,:])
i=i+1
file = open('../data/result.csv', 'w+', newline ='')
header = ["a","b","c","d","e"]
writer = csv.DictWriter(file, fieldnames = header)
# writing data row-wise into the csv file
writer.writeheader()
# writing the data into the file
with file:
write = csv.writer(file)
write.writerows(prefer)
count=count+1
# print(prefer,count) print the outputs step by step
intersection(pre,i,point,count,result)
else:
print("Else Part",type(result))
print(result)
return result
#
pre=["a","b","c"]
rec=intersection(pre)
print(rec)
Output
it prints all the value of result from else part i have excluded it in snapshot because it was too vast and i have few fields here but it wil not effect, for the problem which i am getting... please answer if you know how can i take the value of result into rec.
OK. The code is a bit more complex than I thought. I was trying to work through it just now, and I hit some bugs. Maybe you can clear them up for me.
In the function call, def intersection(pre,i=0,point=0,count=0,result=dt):, dt isn't defined. What should it be?
On the fourth line, i<0 - the default value of i is zero so, unless i is given a value on calling the function, this piece of code will never run.
I notice that the file being read and the file being written are the same: ../data/result.csv - is this correct?
There's another undefined variable, demand, on line 14. Can you fill that in?
Let's see where we are after that.

breaking out of a loop in python

>>> while True:
... line = input('> ')
... if line == 'done' :
... break
... print(line)
... print( 'Done!' )
File "<stdin>", line 6
print( 'Done!' )
^^^^^
SyntaxError: invalid syntax**
For python 3 this code should give me a "Done!" response when the break statement "done" is reached. However, I keep getting a syntax error for the last line. I have tried to do it multiple ways and I can get the break order to work without the last statement, however, it never works where I include the last statement "print('Done!')" with the break statement done. I apologize, this is just simple code but I can't seem to get it to work. Ps, I fixed it to make it as I am supposed to be writing it, the while True statement was not supposed to be twice (my error in copying it here) along with the extra space on the fifth line. and I have tried
It's not the language, it's your IDE. It is only expecting a single statement and the lines that are subordinate to it, i.e. indented. Once you've reached the end of the indenting it expects a blank line and doesn't know what to do when you try to give another statement.
If that same sequence were part of a file it would do exactly what you expect. If you put it all into a function, the whole function body will be indented and that would work too.

Stuck in infinite loop while trying to read all lines in proc.stdout.readline

I am trying to read each line in proc.stdout.readline and send the lines over the network, for example:
data = b''
for line in iter(proc.stdout.readline, ''):
data += line
clientsocket.send(data)
When I run this code I seem to be stuck in a inifinite loop unable to escape to the line:
clientsocket.send(data)
Is there a more efficient way to read the data? I've tried also with a while loop and breaking 'if not line':
data = b''
while True:
line += proc.stdout.readline()
data += line
if not line:
break
clientsocket.send(data)
This seems to also produce the same results. Is there a more efficient way to read all of the data from proc.stdout.readline?
I've encountered the same very problem. The strange thing that in Python 2.7 it had no problem to converge and actually stop iterating.
During debug (in Python 3.5) I've noticed that all true lines returned with the '\n' character, whereas the line that wasn't suppose to arrive returned as an empty string, i.e. ''. So, I just added an if-clause checking against '' and breaking the loop if positive.
My final version looks as follows:
lines = []
for _line in iter(process.stdout.readline, b''):
if _line == '':
break
lines.append(_line)
One thing that might be worth to mention, is that I used universal_newlines=True argument upon subprocess.Popen(..) call.
The statement: iter(proc.stdout.readline, "") will do a blocking read until it recieves an EOF.
If you want to read all the lines, then you can just do:
data = b''
data = b"".join(proc.stdout.readlines())
There is no other solution than for the proc to produce lines faster.
If you want, you can read lines with timeout (i.e. you can wait to read a select number of characters, or timeout if that number of characters are not read).
Those answers can be found here:
https://stackoverflow.com/a/10759061/6400614 .
https://stackoverflow.com/a/5413588/6400614

Python txt file searching not working as expected

The aim of the following snippet of code is to search through a text file, and look for the variable 'name'. If 'name' is present anywhere in the text file, it should set user_new to False. Otherwise, it is non-existent in the txt file, and user_new should be True.
However, user_new, in both cases, is never set to anything, and results in the error: "local variable 'user_new' referenced before assignment"
with open("accuracy.txt", "r") as search:
lineno = 0
for line in search:
lineno = lineno + 1
if name in line:
user_new = False
break
else:
user_new = True
break
search.close()
print(user_new)
The only way this could happen in this code is if the for loop has nothing to iterate over. This would mean the if statement is never run and user_new is never defined.
Make sure accuracy.txt has at least one line, otherwise the loop will never run as there are no lines to loop through.
If having no lines in the text file is unavoidable, consider checking the length of the file with something such as:
if search.read() == "":
user_new = False
Issue was at times accuracy.txt was empty, which caused the loop to never run and user_new to never be defined. Thanks to #Joe Allen for the answer.

Why does error 'unexpected character after line continuation character' appear here?

Does someone know what is wrong with this section of my code as it seems to cause errors. I'm new to programming so I'm not completely sure what's wrong.
menu = "Be Lenny's Friend?\n"
1. Yes\n\
2. No\n\
answer = int(input(menu))
if answer == 1:
print(" ( ͡° ͜ʖ ͡°): Yayyyy! We are going to be friends!")
elif answer == 2:
reason = input(" ( ͡° ʖ̯ ͡°): Why do you not want to be my friend :(")
Error message:
'unexpected character after line continuation character'
Here you have set the variable as a tuple of a string and... well that’s where things get confusing.
The backslash is a like continuation symbol as in you can then break the line and continue on and it would count as the same line. However what the interpreter sees is n: which makes no sense. That is what it is complaining about.
If you wanted to add a new line to the string itself, you could add the \n at the end of the string.
However, also note that if you printed the string using print in the vanilla form without any other arguments than the Adrianne itself, it will append a new line automatically. So if you do add the \n, it may still not be what you want when you print it out. Parameter can of course be changed in the print function to take care of that.
I'm not sure what you are trying to archive with ,\n\: at the end of the line, but this line of code will remove the syntax error:
menu = "Be Lenny's Friend?"
If you want to archive a new line after the string you need to move "\n" into the string like this:
menu = "Be Lenny's Friend?\n"
Edit:
This should work for you:
menu = "Be Lenny's Friend?\n\t1. Yes\n\t2. No\n"
answer = int(input(menu))
if answer == 1: print(" ( ͡° ͜ʖ ͡°): Yayyyy! We are going to be friends!")
elif answer == 2: reason = input(" ( ͡° ʖ̯ ͡°): Why do you not want to be my friend :(\n")

Resources