Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a file in .ktx format. I have opened the file in 'rb' mode. I want to modify particular bytes in that file. I am reading bytes using read(4) [ i want to read number which is of 4 bytes], call and convert each chunk into a number. What I want is, to increase that number by specific number and insert it back into file stream. Is there any function in python which converts a byte string to an integer? I tried with int() but it prints some binary data.
my code:
bytes=file.read(4)
for char in bytes:
print hex(ord(char))
bytes = file.read(4)
bytesAsInt = struct.unpack("l",bytes)
do_something_with_int(bytesAsInt)
I think might be what you are looking for ... its hard to tell from the question though
here is the docs on the struct module https://docs.python.org/3/library/struct.html
Try this
How can I convert a character to a integer in Python, and viceversa?
Here is a suggested workflow for what you seem to be wanting to do
Read the data
Convert the data to integer
Add X to the integer, where X is the value you want to increase by
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
As a French user of Python 2.7, I'm trying to properly print strings containing accents such as "é", "è", "à", etc. in the Python console.
I already know the trick of using u before the explicit value of a string, such as :
print(u'Université')
which properly prints the last character.
Now, my question is: how can I do the same for a string that is stored as a variable?
Indeed, I know that I could do the following:
mystring = u'Université'
print(mystring)
but the problem is that the value of mystring is bound to be passed into a SQL query (using psycopg2), and therefore I can't afford to store the u inside the value of mystring.
so how could I do something like
"print the unicode value of mystring" ?
The u sigil is not part of the value, it's just a type indicator. To convert a string into a Unicode string, you need to know the encoding.
unicodestring = mystring.decode('utf-8') # or 'latin-1' or ... whatever
and to print it you typically (in Python 2) need to convert back to whatever the system accepts on the output filehandle:
print(unicodestring.encode('utf-8')) # or 'latin-1' or ... whatever
Python 3 clarifies (though not directly simplifies) the situation by keeping Unicode strings and (what is now called) bytes objects separate.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a file that contains the raw data for an array of 32-bit floats. I would like to read this data and resemble it to floats and store them in a list.
How can I do this using python?
Note: The data originates from an embedded device that may use a different endian than my desktop.
The standard struct module is good for dealing with packed binary data like this. Here's a quick example:
dataFromFile = "\x67\x66\x1e\x41\x01\x00\x30\x41" # an excerpt from your data
import struct
numFloats = len(dataFromFile) // 4
# Try decoding it as little-endian
print(struct.unpack("<" + "f" * numFloats, dataFromFile))
# Output is (9.90000057220459, 11.000000953674316)
# Try decoding it as big-endian
print(struct.unpack(">" + "f" * numFloats, dataFromFile))
# Output is (1.0867023771258422e+24, 2.354450749630536e-38)
The little-endian interpretation looks a lot more meaningful in this case (9.9 and 11, with the usual floating-point inaccuracies), so I guess that's the actual format.
you can read it the file, store it in a string then parse the string and convert to float:
with open(“testfile.txt”) as file:
data = file.read()
values = data.split(" ")
floatValues = [float(x) for x in values]
or you can use some parser from the numpy module or the csv reading files modules
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This question is not about usage of int function, but rather how it is done internally.
Because source code is in C I don't understand what is going on there.
Maybe someone can explain how Python convert string "123" to integer 123.
What operations are performed for it?
https://github.com/python/cpython/blob/2d305e1c46abfcd609bf8b2dff8d2065e6af8ab2/Objects/longobject.c#L2075-L2366 contains the implementation you're looking for. While understanding the C is useful, there is a large comment in the middle (starting on line 2132) that explains much of the approach.
When converting a python string to an int, e.g. a = int("123",10), (convert the string "123" to an integer in base 10) a C function is is called.
First, it checks that the given counting base base is >= 2 and <=36, or 0. (Error otherwise)
Next, it ignores all leading spaces. (so that " 123" = "123"),
and check if the number is marked as positive '+', or negative '-'
When the base is 0, it checks if the string starts with '0x','0o', '0b', '0', and sets the base respectively (hexadecimal, octal, binary, decimal).
Note that if no base is given, then the default base is 10 (Decimal).
It then proceeds to turning the character array into a number, using the algorithm described in the code comment at the link posted by Paul Kehrer
Trailing spaces are also ignored, and Errors are raised if needed- for example if there's a space in the middle of the string, followed by a number, or if there's a non-number character.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am encountering the following output and I cannot really understand it.
Could you please advise what it is exactly? How to unpack it?
'#\x01\x01\x00'
It does not look to be purely binary or hexadecimal.
I would like to see the ASCII representation of it.
You have a string of bytes, if you print it you are seeing the the ascii output:
In [5]: s = '#\x01\x01\x00'
In [8]: print(list(bytearray(s)))
[64, 1, 1, 0]
If you call chr on each of the ints you will see exactly the same output, 64 in ascii is #, 1 is a SOH and 0 is a NUL , without more info like where it came from there is not much else that can be suggested.
This seems to be a sequence of four bytes with the values 64, 1, 1, 0.
To interpret it, you need to know how it was encoded or what it is supposed to represent.
Generally, you can unpack binary data in Python with the unpack function in the struct module:
import struct
intval = struct.unpack('i', '#\x01\x01\x00')
shortvals = struct.unpack('hh', '#\x01\x01\x00')
The first unpack line would give you the value of your string interpreted as a 4-byte integer, which is the number 65856. The second one interprets the string as two 2-byte integers (320 and 1).
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 years ago.
Improve this question
I have a .txt file that has a really long RNAm sequence. I don´t know the exact length of the sequence.
What I need to do is extract the part of the sequence that is valid, meaning it starts with "AUG" and ends in "UAA" "UAG" or "UGA". Since the sequence is too long I don´t know the index of any of the letters or where the valid sequence is.
I need to save the new sequence in another variable.
Essentially, what you need to do, without coding the whole thing for you, is:
Example string:
rnaSequence = 'ACGUAFBHUAUAUAGAAAAUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAAAAAGGGUAUAUAGAUGAGAGAGA'
You will want to find the index of the 'AUG' and the index of 'UAA', 'UAG', or 'UGA' .. Something like this
rnaStart = rnaSequence.index(begin)
Then you'll need to set the slice of the string to a new variable
rnaSubstring = rnaSequence[rnaStart:rnaEnd+3]
Which in my string above, returns:
AUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAA