Okay so I am trying to write a program that can read lines of RLE and convert them into lines of ASCII Art, I am very new to Python so could anyone help me in understanding what I have done wrong in the code to create this error.
Thanks
It's telling you that you're passing an invalid Integer Literal Value (0xa0) to your function int() in the block of code for b in range (0,int(num))
If you're going to pass a Hexadecimal value to the int function, you need to specify the numeric base (16) as the 2nd parameter of the int function int("0xa0",16)
Related
This Python code gives exactly what I want:
a_hex_str = ':'.join('%02x' % b for b in a_bytes)
which is a string of the byte values in the byte array a_bytes printed in hex with a colon between each value.
I have been going mad trying to get the same output using the new format() function in place of the old % but all my attempts give errors.
I would be very grateful for assistance.
a_hex_str = ':'.join('{}02x'.format(b) for b in a_bytes)
Try this line hopefully it will work.
a_hex_str = ':'.join('{:02x}'.format(b) for b in a_bytes)
I adapted the answer from
https://stackoverflow.com/questions/5661725/format-ints-into-string-of-hex
I didn't realise I needed the colon inside the curly braces, otherwise it gives an out of range error.
I am trying to convert decimal geographic coordinates as strings to a float.
The coordinates are in a csv like this '51213512'. With my Python script I am just reading the coordinates and add the '.'. If I am not adding the comma the rest of my script isn't working.
I already tried a few things but nothing worked for me. This is what I got so far.
latitude=float(long('51.213512'))
The Result is a ValueError:
ValueError: invalid literal for long() with base 10: 'Long'
not too sure why you are using long in this examply if you want to convert this variable to a float just use the float function on its own, you seem to be confusing the long and float functions. dont use both you will be confusing python (basically dosent know what to do because your giving it 2 arguments at once)
I recommend just using the float function on its own. This will avoid confusion
latitude = float('51.2135512')
Get rid of the 'long' and it should work
latitude = float('51.213512')
Edit: Okay, since you're getting the coordinates and manually converting to decimal strings, all you need to do is use the code I said originally. The long function
converts integers or strings of integers to long types, not float types.
>>> long(5)
5L
>>> long('5')
5L
>>> long(5.5)
5L
>>> long('5.5')
ValueError: invalid literal for long() with base 10: '5.5'
I have a follow up question to a post I saw on converting a str() input to a int() type. Based on the definitions of valueError and valueType I would expect the valueType exception to have been used however, it doesn't work (when i tried it). ValueError works but I'm not sure why, isn't int('some string') an example of a wrong type?
Link to original post i'm referring to: Converting String to Int using try/except in Python
From the docs:
class int(x, base=10) Return an integer object constructed from a
number or string x, or return 0 if no arguments are given. If x is a
number, return x.int(). For floating point numbers, this truncates
towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in radix
base. Optionally, the literal can be preceded by + or - (with no space
in between) and surrounded by whitespace. A base-n literal consists of
the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35.
The default base is 10. The allowed values are 0 and 2–36. Base-2, -8,
and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or
0x/0X, as with integer literals in code. Base 0 means to interpret
exactly as a code literal, so that the actual base is 2, 8, 10, or 16,
and so that int('010', 0) is not legal, while int('010') is, as well
as int('010', 8).
When you call the int() function on a string it will try to convert it to the specified base in the arguments (by default base-10) by iterating over the string and converting the string object over to an int object in the desired base. If it reaches a point where the conversion can not be made due to illegal syntax , it will raise a ValueError so terminate the program early. If for some reason you want to go forward you can but a try: except block in the code to catch the exception.
From the Docs:
exception ValueError Raised when a built-in operation or function
receives an argument that has the right type but an inappropriate
value, and the situation is not described by a more precise exception
such as IndexError.
Hello people,
I've got a problem with my code. For some reason the values are not converted to integers from strings and are not adding up. Here is my code.
def SumOfState(i,j):
cf=readPopest(file1)
sum2=[]
sum7=[]
Diff=0
for y in range((j)):
StateList=str(cf[y+i]).split(',')
sum2.append(StateList[2])
sum7.append(StateList[7])
results2 = [int(i) for i in sum2]
results7 = [int(i) for i in sum7]
print sum(results2)
print sum(results7)
Error message : Inappropriate argument value (of correct type).
An error occurred attempting to pass an argument to a function.
cf=readPopest(file1)
the code ^^ gives a list containing words and numbers. One element is taken % split into sublists.
Ive tried the int() function and the for loop variant of it.
suggest me an edit, please.
Really appreciate any help.
Thanks.
-Addie Vanhala
I guess looking at you code, it is because sum2 and sum7 contain non integers, probably because some part of file1 (accessed though readPopest) is non int.
I am trying to read a tag from XML and then want to concatenate a number to it.
Firstly, I am saving the value of the string to a variable and trying to concatenate it
with the variable in the for loop. But it throws an error.
for i = 0:tag.getLength-1
node = tag.item(i);
disp([node.getTextContent]);
str=node.getTextContent;
str= strcat(str, num2str(i))
new_loads = cat(2,loads,[node.getTextContent]);
end
Error thrown is
Operands to the || and && operators must be
convertible to logical scalar values.
Error in strcat (line 83)
if ~isempty(str) && (str(end) == 0 ||
isspace(str(end)))
Error in SMERCGUI>pushbutton1_Callback (line 182)
str= strcat(str,' morning')
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in SMERCGUI (line 44)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)SMERCGUI('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
The error suggests that your string is not a string. It's not clear to me whether it's throwing an error at the strcat line, or at the later cat line.
At any rate, it should be clear that you cannot concatenate elements of different types into an array - cell array yes, regular array no. So the line
new_loads = cat(2,loads,[node.getTextContent]);
is bound to give a problem. 2 is numerical, and node.getTextContent is a string - or maybe a cell array or something else. I can't see what loads is, so I can't tell if that is involved in the problem.
Usually a good way to combine numbers and strings into a single string is
newString = sprintf('%s %d', oldString, number);
You can then use all the formatting tricks of printf to produce output exactly as you want. But before you do anything, make sure you understand the type of all the elements you are trying to string together. The easiest way to do this for all the elements in memory is
whos
Or if you just want it for one variable,
whos str
Or all variables starting with s:
whos s*
The output is self-explanatory. If you still can't figure it out after this, leave a comment and I'll try to help you out.
EDIT based on what I read at http://blogs.mathworks.com/community/2010/11/01/xml-and-matlab-navigating-a-tree/ , it is possible that you just need to cast your str variable to a Matlab string (apparently it's a java.lang.string). So try to add
str = char(str);
before using str. It may be what you need.