1

I'm a total beginner in python and i have a set of byte values in a CSV file which i want to process. Sample values are mentioned below

"b'\xaa'"
"b'\x04'"
    data1 = pd.read_csv("test.csv", usecols=[1])
    for value in data1.values.flatten():
       print(int.from_bytes(value, byteorder='big'))

When running the above code i get the error saying

TypeError: cannot convert 'str' object to bytes

because it is read as a string. How could I pass this string as bytes and use it in the above code?

I'm using Python 3.7.

1 Answer 1

1

You can try

>>> x = "b'\xaa'"
>>> int.from_bytes(x.encode('utf-8'), byteorder="big")
421573863975
>>> x = "b'\x04'"
>>> int.from_bytes(x.encode('utf-8'), byteorder="big")
1646724135
Sign up to request clarification or add additional context in comments.

2 Comments

I suspect this is the wrong answer because presumably this string is created by doing str(int.to_bytes(170, length=1, byteorder='big')). The reverse would be extracting \xaa and finding the integer value of that (which is 170).
Yes blueteeth is correct. This answer is wrong. If the string is "b'\xaa'" I want to retrieve 170.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.