I keep getting ValueError in my Python program - python-3.7

I'm trying to open a csv file for my python book project and this error keeps popping up
File "c:\Users\MSFT Surface Pro 3\Documents\Programming\Python\Python AIO\Code.py", line 13, in <module> birthYear = int(row[1] or 0) ValueError: invalid literal for int() with base 10: ' 1/11/2011'
and this is super annoying, help me please!

Looks like line 13 in in Code.py is birthYear = int(row[1] or 0), but probably should be something like birthYear = row[1] or "".

Related

Problems with "match" function in PyDroid3

Creating a todo list that uses the "match" function:
while True:
user_action = input("Type add, show, edit, or exit: ")
user_action = user_action.strip()
match user_action:
case "add":
todo = input("Enter a todo: ")
todos.append(todo)
file = open("todos.txt", "w")
file.writelines(todos)
Here is what happens:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 7
match user_action:
^
SyntaxError: invalid syntax
I suspect that there is no "match" function in PyDroid. Sorry, but I'm quite new at this. Is there something I'm missing or some way I can make a work-around? Thanks in advance!
It seems the real problem here is that the version of PyDroid is 3.9.7, which is to say, it does not contain the "match" function.

What should I do to highlight error lines after running code in VSCode?

I want to highlight error line when I finish running code in vscode. Just like this code I wanna highlight 45 and 57 lines. What can i do?
Traceback (most recent call last):
File "f:\onedrive\L_ML\L_ML_W3_Lab06-09\L_ML_W3_Lab06.py", line 57, in <module>
dj_db_tmp, dj_dw_tmp = compute_gradient_logistic(X_tmp, y_tmp, w_tmp, b_tmp)
File "f:\onedrive\L_ML\L_ML_W3_Lab06-09\L_ML_W3_Lab06.py", line 45, in compute_gradient_logistic
dj_dw = dj_dw[j] + err_i * X[i,j]
IndexError: invalid index to scalar variable.
like this picture
use yellow highlight error line

.extractText() returns "invalid literal for decimal"

I'm coding something which will read PDFs online and return a set of keywords that are found in the document. However I keep running into a problem with the extractText() function from the PyPDF2 package.
Here's my code to open the PDFs and read it:
x = myurl.pdf
if ".pdf" in x:
remoteFile = urlopen(Request(x, headers={"User-Agent": "Magic-Browser"})).read()
memoryFile = StringIO(remoteFile)
pdfFile = PyPDF2.PdfFileReader(memoryFile, strict=False)
num_pages = pdfFile.numPages
count = 0
text = ""
while count < num_pages:
pageObj = pdfFile.getPage(count)
count += 1
text += pageObj.extractText()
The error that I keep running into on the extractText() line goes like this:
Traceback (most recent call last):
File "errortest.py", line 30, in <module>
text += pageObj.extractText()
File "/anaconda2/lib/python2.7/site-packages/PyPDF2/pdf.py", line 2595, in extractText
content = ContentStream(content, self.pdf)
File "/anaconda2/lib/python2.7/site-packages/PyPDF2/pdf.py", line 2674, in __init__
self.__parseContentStream(stream)
File "/anaconda2/lib/python2.7/site-packages/PyPDF2/pdf.py", line 2706, in __parseContentStream
operands.append(readObject(stream, None))
File "/anaconda2/lib/python2.7/site-packages/PyPDF2/generic.py", line 98, in readObject
return NumberObject.readFromStream(stream)
File "/anaconda2/lib/python2.7/site-packages/PyPDF2/generic.py", line 271, in readFromStream
return FloatObject(num)
File "/anaconda2/lib/python2.7/site-packages/PyPDF2/generic.py", line 231, in __new__
return decimal.Decimal.__new__(cls, str(value))
File "/anaconda2/lib/python2.7/decimal.py", line 547, in __new__
"Invalid literal for Decimal: %r" % value)
File "/anaconda2/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: '99.-72'
Would be great if someone could help me out! Thanks!
There is too little information to be certain, but PyPDF2 (and now pypdf) improved a lot in 2022. You will probably just need to upgrade to the latest version of pypdf.
If you encounter a bug in pypdf again, please open an issue: https://github.com/py-pdf/pypdf
A good bug ticket contains (1) your pypdf version (2) the code + PDF document that caused the issue.

pyang and JNC integration

I'm trying to integrate JNC and Pyang. As the jnc steps describes I have copied jnc.py under PYANG_HOME/pyang/plugins. I try to generate the java classes for simple.yang under $JNC_HOME/examples/yang using the command
pyang -f jnc --jnc-output src/gen/simple yang/simple.yang
facing the following error,
Traceback (most recent call last):
File "D:/tools/pyang-master/bin/pyang", line 434, in <module>
run()
File "D:/tools/pyang-master/bin/pyang", line 408, in run
emit_obj.emit(ctx, modules, fd)
File "C:\Users\Siva\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyang-1.7-py3.5.egg\pyang/plugins\jnc.py", line 208, in emit
if module_stmt in (imported + included):
TypeError: unsupported operand type(s) for +: 'map' and 'map'
Anyone faced this kind of issue. please let me know how to fix this.
Problem is with map implementation:
map in Python-3 returns an iterator, while map in Python 2 returns a list:
Python 2:
>>> type(map(abs, [43, -12, 13, -14]))
<type 'list'>
Python 3:
>>> type(map(abs, [99, -52, 32, -34, 13]))
<class 'map'>
You can edit file jnc.py and change code as below:
for (module_stmt, rev) in self.ctx.modules:
if module_stmt in (imported + included):
module_set.add(self.ctx.modules[(module_stmt, rev)])
for (module_stmt, rev) in self.ctx.modules:
if module_stmt in (included):
module_set.add(self.ctx.modules[(module_stmt, rev)])
if module_stmt in (imported):
module_set.add(self.ctx.modules[(module_stmt, rev)])

Jython 2.5 isdigit

I am trying to add an isdigit() to the program so that I can verify what the user enters is valid. This is what I have so far. But when I run it an enter a character, say "f". It crashes and gives me the error which will be posted below the code. Any ideas?
def mirrorHorizontal(source):
userMirrorPoint = requestString("Enter a mirror point from 0 to halfway through the pitcure.") #asks user for an input
while (int(userMirrorPoint) < 0 or int(userMirrorPoint) > (int(getHeight(source) - 1)//2)) or not(userMirrorPoint.isdigit()):
userMirrorPoint = requestString("Enter a mirror point from 0 to halfway through the pitcure.")
height = getHeight(source)
mirrorPoint = int(userMirrorPoint)
for x in range(0, getWidth(source)):
for y in range(0, mirrorPoint):
topPixel = getPixel(source, x, y)
bottomPixel = getPixel(source, x, height-y-1)
color = getColor(topPixel)
setColor(bottomPixel, color)
The error was: f
Inappropriate argument value (of correct type).
An error occurred attempting to pass an argument to a function.
Please check line 182 of /Volumes/FLASHDRIVE2/College/Spring 16'/Programs - CPS 201/PA5Sikorski.py
isdigit() itself behaves itself in the 2.7.0 jython version I have locally
>>> '1'.isdigit()
True
>>> ''.isdigit()
False
>>> 'A'.isdigit()
False
>>> 'A2'.isdigit()
False
>>> '2'.isdigit()
True
>>> '22321'.isdigit()
True
Try breaking your big expression up, as typecasting to integers will throw errors for non-numeric strings. This is true across Python versions.
>>> int('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'b'
>>> int('2')
2
You probably want to be careful about the order of the parts of that long expression (this or that or ...). Breaking it up would also make it more readable.