strftime returns Value error. Why?
This is the code
datetime.datetime.now().strftime('%-m')
My output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format string
Based on strftime.org format can depend on system.
On Unix it may needs - like "%-m" but on Windows it may need # like "%#m".
I could test it only on Linux and both works for me: "%-m %#m"
BTW: but if you want to get negative value then you need - before % like "-%m"
Related
How to get the precise position of an error within the line in Python? The Python interpreter gives the line of the error and the type of the Error, but if there are more points in the line that could cause that error then there is ambiguity, here is a toy example:
example.py
xs = []
ys = {"item": xs}
zs= {"item":ys}
print(zs['item']['item']['item'])
Where the error is:
Traceback (most recent call last):
File "p.py", line 4, in <module>
print(zs['item']['item']['item'])
TypeError: list indices must be integers or slices, not str
Here, considering that xs, ys and zs could be the result of long computation, it could not be clear which one of the ['item'] triggered the TypeError.
I would prefer an error message like:
Traceback (most recent call last):
File "p.py", line 4, in <module>
print(zs['item']['item']['item'])
^-------
TypeError: list indices must be integers or slices, not str
That tells me that the problem is in the last accessing with ['item'].
I am using Python 3.8.16
This feature has been added in Python 3.11.1, in this new version the error message is:
Traceback (most recent call last):
File "/content/p.py", line 4, in <module>
print(zs['item']['item']['item'])
~~~~~~~~~~~~~~~~~~^^^^^^^^
TypeError: list indices must be integers or slices, not str
Telling you that the error is caused by the third ['item'].
You can install it on a Linux system with:
apt install python3.11
And run a python file with the new Python version with:
python3.11 example.py
I was running this code on VScode.
age_1 = int(input("age : "))
print(age_1)
It shows this at it's terminal output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'print(age_1)
In Pycharm it shows this output after taking input as 12 :
age : 12
12
In PyShell it works like the Pycharm one. What did I do wrong in the VScode ?
Update :
When ran on a new file on VScode, it ran properly like the Pycharm one. But only for once . Then it shows up same error like before along with some new kinds of error everytime it ran.
Here's the correct one in VScode when ran the 1st time.
PS I:\CSE\LEARNING\PYTHON 3.0\practice> & "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
age : 12
12
Here's one error when ran another time.
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
File "<stdin>", line 1
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
^
SyntaxError: invalid syntax
Here's another error :
>>>print(age_1) age1 = int(input("age : "))
File "<stdin>", line 1
print(age_1) age1 = int(input("age : "))
^
SyntaxError: invalid syntax
& here's the final one .
>>> print(age1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'age1' is not defined
P.S. I ain't skilled in coding. Explaining with some codes patiently will be appretiated.
The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. ... Then, you can use int() to convert your number to an integer. If this does not work, make sure that the value of a string does not contain any letters.I think you wrote a word that does not change int in the input. Maybe it's a space
I'm trying to convert a hex string 'aa' to binary as following:
a = bin(int('aa',16))
But it gives me the error of:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
a = bin(int('aa',16))
TypeError: bin(QTextStream): argument 1 has unexpected type 'int'
Can anyone explain what is the problem with the conversion?
You did some sort of import *, probably
from PyQt4.QtCore import *
causing the built-in bin to be shadowed by a different function. Stop using import *, and the problem will go away.
So I'm trying to get to grips with using the rpy2 module (I am familiar with R but new to Python). Following this tutorial, I first load the library and assign it to the variable 'r' using:
import rpy2
import rpy2.robjects as robjects
r = robjects.r
then I try to perform a simple operation to confirm everything is working:
print(r[2+2])
but I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\rpy2\robjects\__init__.py", line 248, in _
_getitem__
res = _globalenv.get(item)
TypeError: argument 1 must be str, not int
I'm sure it's just something stupid I'm doing wrong, but any advice would be much appreciated. I'm using python3.4.2 (64bit), rpy2-2.5.6 (64bit) on a Windows 7 machine (64bit).
You should use print(r(2+2)) instead of print(r[2+2]).
When you use r[2+2] you are trying to recover an element corresponding to the index 4 (the result of 2+2) of the r iterable. And your r object doesn't seem to respond to this kind of message.
Ok I think I have figured it out. For R to evaluate the function inside the parenthesis, the function must be in quotes e.g.
r("2+2")
This is what was confusing me because this looks like I'm providing a string.
Oddly I don't print the result (4) by using:
print(r("2+2"))
as this prints:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
print(r("2+2"))
File "C:\Python34\lib\site-packages\rpy2\robjects\robject.py", line 49, in __str__
s = str.join(os.linesep, s)
TypeError: sequence item 0: expected str instance, bytes found
Instead I just print the result using:
answer = r("2+2")
answer[0]
(Because R is vector based, the initial value of the vector is the answer so you have to index it at the first position, otherwise you get:
answer = r("2+2")
answer
<FloatVector - Python:0x0000000005836EC8 / R:0x00000000047A51A0>
[4.000000]
Thanks for you help
Hefin
The trivial
import Image
im = Image.OPEN('C:\abc.bmp')
results in the following exception
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
im = Image.OPEN('C:\Documents and Settings\umair.ahmed\My Documents\My Pictures\avanza.bmp')
TypeError: 'dict' object is not callable
not sure if i am missing something, kindly help.
Use:
Image.open()
It's case-sensitive.
I don't think the error message came from your input, because the file names are different, but you should not use 'C:\abc.bmp', in your open() call, but use either C:/abc.bmp, or r'C:\abc.bmp'. Backslash is an escape character in Python.