How to get rid of the error 'EOFError: EOF when using input() after having used sys.stdin.readlines() in a different function? - python-3.x

I am trying to replicate an Excel VBA macro to Python for the sake of learning a new programming language and I am stuck at a point which Google alone is not helping (I guess I do not even know what to look for). Could you please give it a try?
What I expect the program to do:
When running the user should be prompted with a few options and if the input is 0 then it should ask for a multi line input containing a full HTML source code from this website Steam Tools
After the input the user is expected to hit CTRL+D / CTLR+Z to confirm there is nothing else to add (I think the problem is here, maybe it is not able to "clear" the EOF error while using input() again?)
Then as an ouput the program should return the first 10 rows delimited by comma and create another input() to avoid the window to autoclose.
What the issue is:
When I run it from the desktop (double cliking the .py file) it autocloses withouth creating the last input().
When I run it from PyCharm it runs OK and the last input remains waiting for user action. However, it does dump an error like this:
File "D:/Stuff/_root/py/Steam/steam_cards_manager.py", line 51, in z_parse_tbody
input('\nCopy the program output and type Back:') EOFError: EOF when reading a line
Any feedback is appreciated as I don't know if I am doing things in an easy / effcient way.
I've upload my .py file and also a sample HTML to make it easier to replicate the issue, hope it helps.
https://github.com/brmenezewe/db

It turned out that I had to replace the CTRL+D shortcut by a "trigger" word that when sent via a single input() it breaks the While loop and joins the inputs previously received:
def z_input_lines():
lines = []
while True:
line = input()
if not line or line.lower() != "go":
lines.append(line)
else:
break
return "".join(lines)

Related

How do I close a file in the terminal

I've only started doing python3 for a paper at university. I don't plan on continuing after this semester is over but I want to do the best I can in this class. However, I am having difficulties with seemingly a basic task.
I've opened a file using f = open() and I've accessed it in the terminal using less. It displays everything out nicely but it doesn't let me close the file or even continuing to code past the printed text.
It just repeats ~ on separate lines and finishes with a highlighted (END)
By typing q, you will be able to exit it

How do I convert a file from plain text to a list variable?

I have a python program and it seems that whenever I open it with this line of code active:
content = open('Word_list.txt').read().splitlines() it just closes itself (by open I mean double clicking it from file explorer, works fine every other way). I tried with a small list variable and my program works file when it's like that so I need to convert a 4MB file (4,250,000 lines) into a python list variable. Is there any way to do that and will there be any slowdowns because of what i'm doing here?
Oh, and I should mention that I tried other ways of importing the text file (for loops and whatnot) and that seemed to crash instantly too. Thanks!
Because you are running it through Explorer the window is closing before you can see an error message, so add some error handling code:
try:
content = open('Word_list.txt').read().splitlines()
except Exception as exc:
import traceback
print(traceback.format_exc())
input()
I managed to use Notepad++ and their macro system to put the required " ",'s over all of the words, completing my objecting using another program instead of python.

Getting an invalid syntax <Unknown, line 6> error for print function w/ Pylint on VSCode

For my weekly course assignment, we're tasked w/ creating simple user entry points for a fictional employee. I ran the code once and everything worked out as I needed it to w/ it asking the user to input data, once input it presented it w/ a title. I forgot to add a field for the employeeName and the "----" lines, so I added all of that in, copy and pasting some of it to get done as the code is highly similar. I then got the syntax error. I checked my spelling for print, deleted the print function, and then used VS Code intellisense to autofill the function, no changes to the error, went through the line to see if I forgot something or added extra nonsense. I looked through similar posts, but they aren't quite getting at my specific issue from what I can tell. Any help is much appreciated.
Regards
# user input for an employee's SSN
print("Please enter your SSN #:", end=' ')
employeeSSN = input(str())
print('SSN:', employeeSSN)
print("PLease enter your SSN #:", end=' ')
^
SyntaxError: invalid syntax
Expected results should print the string inside quotations and allow for user input.
I found my own error after some more checking and testing. I forgot to put in an ending parenthesis for line 4 of my code. I found it by taking away the last set of things I worked on and retesting to a good point, then slowly re-adding in the rest. I noticed the missing parenthesis in that process. I wish the error would have pointed my towards that missing part in line 4 then on line 6 and the function. Had me thinking something totally different.
Example of code mess-up
print("----------------------------", employeeName, "----------------------------"
How I fixed it
print("----------------------------", employeeName, "----------------------------")
Sometimes, its the simplest things right in our face. Hopefully this helps someone else.

Subprocess STDOUT is not Live

After looking through pretty much all relevant articles and trying everything, I can't make this work, so many thanks in advance for taking a look.
I wrote the following code to activate second script from the first script:
proc = subprocess.Popen(['python', path_calculator +
'calculator.py'], stdout=subprocess.PIPE)
for line in proc.stdout:
sys.stdout.buffer.write(line)
sys.stdout.buffer.flush()
line = str(line)
line = line[2:]
line = line[:-3]
The point of this code is to get the live output of the 'calculator' in the first script, because other scripts are depending on that output.
The code does provide the correct output, but only when the 'calculator' finishes. It then outputs all data in one go. That's not sufficient. I need the output the moment it comes out of the 'calculator'.
Funny thing is that it actually works on my mac, but I can't get it to work on Windows.
** A few hours later now, I found out that it's not my Mac that it works on, but it's Pycharm (which I use on my mac) that it works on. If I run it in the terminal on my mac it doesn't work either.
Any suggestions?

VBA how to run a string as a line of code

Is there a way to convert a string into an executable line of code?
something like:
Dim Line1 as String
Line1 = "MsgBox (""Hello"")"
Execute Line1
resulting in the pop up box saying Hello.
What I'm doing is pulling lines out of a text file. I can pull lines that are the names of form controls and use them to perform actions on those form controls but I'd like to go one step further and execute lines.
I've seen claims that this would work:
Application.Run Line1
or make the variable an array and store it in element 1 e.g. and use
Application.Run Line1(1)
but it doesn't work for me.
Ok, while writing this I've also been experimenting. I found that
Eval (Line1)
will work when Line1 is a message box, but not when it is something like:
line1 = "DoCmd.OpenForm ""form1"""
Any tips would be appreciated.
Thanks
You can use,
Eval("DoCmd.OpenForm(""form1"")")
You have to make sure any functions you include use parentheses.
Further reference,
http://msdn.microsoft.com/en-us/library/office/aa172212(v=office.11).aspx
It's not exactly what I was asking, I ended up going a slightly different direction, but here's what I ended up doing and it would probably be easily altered to more closely match my question. I actually took lines of text from an external text file and inserted them into a line of code. What I was doing in this example was just hiding columns, the external text file was a list of column names. (figuring out how to output that was fun too)
Open "C:\UserList.txt" For Input As #TextFile
While Not EOF(TextFile)
Line Input #TextFile, TextLine
Screen.ActiveDatasheet.Controls(TextLine).ColumnHidden = True
Wend
Visual Basic is a compiler language, and as such does not support the ability to execute human-readable code while it is running. All code written in VBA must first be compiled BEFORE the program runs the first time.
However, SQL is an interpreter language, and can be handed code which it will execute line by line. You can also fetch contents for variables from other sources while the program runs, you just can't build a string while the program is running and then execute it in VBA directly.

Resources