Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm using struct to parse fixed width strings. However, I'm having some trouble with dealing fixed width strings larger than 1000 bytes.
For example, when I execute the following code:
import struct
fmt = '2s25s16s1s40s2s1s1s2s9s1s6s10s25s2s2s9s8s2s2s4x8s2x2s2s2s2s2s1x13s6s2s2s2s2s1x3s4s6s4s12x1s2s1x7s1s2s2s2s2s2s1x3s6x2s2x2s2x2s2x2s2x2s2x2s6s2x1s4x4s2s2s2s2s2s2s2s8x3s3s3s3s3s3s3s3s3s2s2s2s2s2s2s8s2x2s2s2s2s2s150s50s4x1s2s8s15x30s30s10s15s15s10s10s10s10s12s3s3s3s3s1s3s3s1x15s2s3s8s2s2s2s2s2s16s2s3s2x2s3s2x1s1s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s10x2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s2s2s3s3s42s6s21s3s3s3s3s3s3s3s3s3s3s3s3s7s'
parse = struct.Struct(fmt).unpack_from
line = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ123456'
parse(line.encode())
I get the following error:
Traceback (most recent call last):
File "so.py", line 6, in <module>
parse(line.encode())
struct.error: unpack_from requires a buffer of at least 1164 bytes
I've been searching for ways to set the buffer to 1164 bytes with no success.
Let's take a look to https://docs.python.org/3.0/library/struct.html#struct.unpack_from first, it says:
Unpack the buffer according to tthe given format. The result is a
tuple even if it contains exactly one item. The buffer must contain at
least the amount of data required by the format (len(buffer[offset:])
must be at least calcsize(fmt)).
Let's try first calcsize on your fmt, print(struct.calcsize(fmt)) says 1164.
Now let's see the len of your buffer len(line), it says 1050.
So the error is because you're not following the guidelines provided by the docs...
PS: parse((line+line[0:struct.calcsize(fmt)-len(line)]).encode())
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I would like to know how to write in a single line the combination of two hash algorithms in Python. I want to use MD5 and sha256 in one line. Here is my function:
def calcHash(self):
block_string = json.dumps({"nonce":self.nonce, "tstamp":self.tstamp, "output":self.output, "prevhash":self.prevhash}, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
I tried return md5.hashlib(hashlib.sha256(block_string).hexdigest()) but it doesn't work.
Can someone help?
The problem could just be that .hexdigest() produces a string, not a bytes, so you need to encode it again
>>> import hashlib, json
>>> j = json.dumps({"foo":"bar","baz":2022})
>>> hashlib.sha256(j.encode()).hexdigest()
'44ce0f46288befab7f32e9acd68d36b2a9997e801fb129422ddc35610323c627'
>>> hashlib.md5(hashlib.sha256(j.encode()).hexdigest().encode()).hexdigest()
'f7c6be48b7d34bcd278257dd73038f67'
Alternatively, you could directly use .digest() to get bytes from the hash, rather than intermediately converting it to a hex string (though note the output is different)
>>> hashlib.md5(hashlib.sha256(j.encode()).digest()).hexdigest()
'12ac82f5a1c0cb3751619f6d0ae0c2ee'
If you have a great many algorithms in some order, you may find .new() useful to iteratively produce a result from some list of hashes.
BEWARE
md5 is broken from a security perspective and I cannot recommend using it unless it's a required input to a legacy system!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am going to get a number from a user and find the nearest number that is larger and multiple to 10. Here is my code:
n = int(input(''))
n + (10 - n%10)
but when I run it I come across this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: n + (10 - n%10)
What am I doing wrong?
My python version is 3.9.6
Your code works like it is supposed.
The problem is that you didn't input an integer and instead inputted your second line:
n + (10 - n%10)
P.S.: I would consider changing your code to the following, it uses only one line instead of two, but that's up to you:
n = int(input(''))//10*10+10
It seems that you are giving the mathematical expression as input, rather than a number. When you call input and wrap it with int(), python expects to take the input and convert it to a number which belongs to base 10. Obviously any non-numeric string doesn't belong to base 10.
So you should write the second line after the input one, and wrap it with a print() to display the result. If you are writing this in the interpreter, then you should immediately provide the number you want to test, and then give python the math expression to calculate
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Can't understand the error in this as the brackets are as per directions
mean=df["Normalized-losses"].mean()
Traceback (most recent call last):
File "C:\Users\Aarushi Goyal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\nanops.py", line 822, in _ensure_numeric
x = float(x)
Please provide solution
Try converting the column into numeric using
pd.to_numeric(df['Normalized-losses'], errors = 'coerce')
Then try:
mean = df['Normalized-losses'].mean()
You can also use:
mean = df.loc[:, 'Normalized-losses'].mean()
If it doesn't help do provide more info regarding the error.
I think the variable "Normalized-losses" would be a non-numerical type variable.
Try pandas dtypes method to check data type:
df.dtypes
If its non-numerical then use astype() method to change the data type.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm running a snippest like that:
p = re.compile(b'^((?!-)[*A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$')
m = p.match(domain)
'domain' are the ip addresses get from google dns. I know there is something wrong with the decoding, so I've tried to encode the regular expressions after it was compiled, but still get the bug notice like this,
Traceback (most recent call last):
File "D:\python34\lib\threading.py", line 921, in _bootstrap_inner
self.run()
File "update.py", line 101, in run
if validate_domain(domain):
File "update.py", line 182, in validate_domain
m = pattern.match(domain)
TypeError: can't use a bytes pattern on a string-like object
Could you give me some tips for this situation
!!!Programming language: python 3.4
Appending a b to the start of your pattern makes it a bytes object. But you can't match string objects with a bytes pattern. The error is quite clear:
p = re.compile(b'^((?!-)[*A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$')
# ^
You probably intended to use r''
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to compare two list of lists if they are equal.
if grafo.node[va,vb] == grafo.node[va,vb]:
I get this error:
Traceback (most recent call last):
File "C:/Python33/Archive/PythonGrafos/Alpha.py", line 85, in <module>
menugrafos()
File "C:/Python33/Archive/PythonGrafos/Alpha.py", line 55, in menugrafos
Beta.criararesta(grafo,va,vb)
File "C:/Python33/Archive/PythonGrafos\Beta.py", line 29, in criararesta
if grafo.node[va,vb] == grafo.node[va,vb]:
TypeError: list indices must be integers, not tuple
I'm inserting integers in the lists. What does this error mean?
The error suggests that va and vb are strings, so you cannot use them as indexes. If they contain some integer you want to use for index, then use [int(va)][int(vb)] and it will probably work.
Also interjay is right your code is different than the traceback!
What is in va and vb? It needs to be an int, assuming that node is a list. If you do want it to use a string, as an index, use a dict instead.
What you probably want to do is:
grafo.node[int(va)] == grafo.node[int(vb)]