I am trying to convert a list of integers to bytearray in Python 2.7
input code:
bytearray([1,2,120,120])
output:
bytearray(b'\x01\x02xx')
But when I try to "print" the same bytearray, I am getting some wired characters.
input code:
print bytearray([1,2,120,120])
output:
??xx
Are they both same? What is the reason behind this?
If you want to get bytearray(b'\x01\x02xx') you need to use the __repr__ method of a bytearray object:
print repr(bytearray([1,2,120,120]))
By default __str__ in invoked, that returns a string representation of a byte array. And that string contains the \x01\x02 that are not printable characters, hence you see question marks.
References:
https://docs.python.org/2/library/functions.html#repr
Related
On Selenium, I'm writing script to get the number from the text. suppose there is a field 'Status(2)'. The number in the brackets keep changing. I want to get the value.
This code should get back the text for the element you have provided:
WebElement web_element_found = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects"));
String element_text = web_element_found.getText();
Then you can have a look at this answer for how to use regex to extract the digit from the string: Regex to extract digit from string in Java
Hope this helps!
Here is the solution.
String rawText = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects")).getText();
String number = rawText.substring(s.indexOf("(") + 1).substring(0, s.indexOf(")"));
System.out.println(number);
Problem:
Hello, I have been struggling recently in my programming endeavours. I have managed to receive the output below from Google Speech to Text, but I cannot figure out how draw data from this block.
Excerpt 1:
[VoiceMain]: Successfully initialized
{"result":[]}
{"result":[{"alternative":[{"transcript":"hello","confidence":0.46152416},{"transcript":"how low"},{"transcript":"how lo"},{"transcript":"how long"},{"transcript":"Polo"}],"final":true}],"result_index":0}
[VoiceMain]: Successfully initialized
{"result":[]}
{"result":[{"alternative":[{"transcript":"hello"},{"transcript":"how long"},{"transcript":"how low"},{"transcript":"howlong"}],"final":true}],"result_index":0}
Objective:
My goal is to extract the string "hello" (without the quotation marks) from the first transcript of each block and set it equal to a variable. The problem arises when I do not know what the phrase will be. Instead of "hello", the phrase may be a string of any length. Even if it is a different string, I would still like to set it to the same variable to which the phrase "hello" would have been set to.
Furthermore, I would like to extract the number after the word "confidence". In this case, it is 0.46152416. Data type does not matter for the confidence variable. The confidence variable appears to be more difficult to extract from the blocks because it may or may not be present. If it is not present, it must be ignored. If it is present however, it must be detected and stored as a variable.
Also please note that this text block is stored within a file named "CurlOutput.txt".
All help or advice related to solving this problem is greatly appreciated.
You could do this with regex, but then I am assuming you will want to use this as a dict later in your code. So here is a python approach to building this result as a dictionary.
import json
with open('CurlOutput.txt') as f:
lines = f.read().splitlines()
flag = '{"result":[]} '
for line in lines: # Loop through each lin in file
if flag in line: # check if this is a line with data on it
results = json.loads(line.replace(flag, ''))['result'] # Load data as a dict
# If you just want to change first index of alternative
# results[0]['alternative'][0]['transcript'] = 'myNewString'
# If you want to check all alternative for confidence and transcript
for result in results[0]['alternative']: # Loop over each alternative
transcript = result['transcript']
confidence = None
if 'confidence' in result:
confidence = result['confidence']
# now do whatever you want with confidence and transcript.
After separating fields with
var fields = row.characters.split {$0 == "\t"}.map { String($0) }
the debugger shows "unable to read data" for one field.
What does it mean, except there is no data? I want to check it, but it is no empty string ("") and it's not nil.
Are you using Swift 2 in XCode 7? Might wanna give this a try:
var fields = row.characters.split {"\t"}.map(String.init)
Or maybe your problem is that row ends with a "\t" and the final string is not valid for String init. I would suggest split and then print your results as debug.
source: Ethan's answer
when using the ETCD dashboard, we find a binary string like
var _deps_js = "\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xcc\xbd\x7b\x77\xdb\x46\xb2\x2f\xfa\xff\xf9\x14\x22\xc6\x47\x01\xcc\x16\x25\xd9\x99\xb9\x7b\xc0\x40\xbc\x89\x1d\x27\xce\x24\x76\x26\x76\x26\x0f\x8a\xc9\xc2\x8b\x24\x24\x92\xa0\x49\xca\x92\x23\x72\x3e\xfb\xad\x5f\x55\x77\xa3\x01\x82\x72\xf6\xec\x7b\xd6\x3a\x99\xb1\x88\x47\xa3\x9f\xd5\xd5\xf5\xae\xce\xf8\x66\x91\x6e\x8a\x72\xe1\xc7\x2a\x09\xee\xbd\x32\xb9\xca\xd3\x8d\x17\x45\x9b\x0f\xcb\xbc\x1c\x1f\xcd\xcb\xec\x66\x96\x1f\x1f\x1f\x78\xd1\xcb\xef\x96\xe5\x6a\xb3\x1e\xd4\x6f\xa3\xb8\x97\x95\xe9\xcd\x3c\x5f\x6c\x06\x09\xd5\xdc\x39\x0b\xc2\xaa\xa1\xe0\xbe\x18\xfb\x9d\xaa\x48\xb0\x99\xae\xca\xdb\xa3\x45\x7e\x7b\xf4\xe5\x6a\x55\xae\x7c\xef\xea\x9f\x37\xf9\xea\xc3\xd1\x2a\x7f\x77\x53\xac\xf2\xf5\x51\x7c\x74\x5b\x2c\x32\x2a\x73\x5b\x6c\xa6\x74\x67\xbe\xf4\x82\xfe\x2a\xdf\xdc\xac\x16\x47\xd4\x4a\xb0\x0b\xf9\xaf\xef\xdd\x2c\xb2\x7c\x5c\x2c\xf2\xcc\xeb\x98\xee\xca\xf7\x03\xf9\x09\x37\xd3\x62\xad\xea\x23\x37\x77\x47\x29\x3a\xf8\x3e\x5e\x1d\x25\x34\x8a\x59\xbe\x98\x6c\xa6\x2a\x8d\xe2\xa4\x87\x9a\xe8\x9d\x6e\xd1\x33\x1f\xd0\x94\x44\xe9\x76\x4b\x05\x8a\xf5\x4f\x5c\x3d\x15\x1a\x74\xce\xc3\x73\x7a\x11\xf7\x16\x65\x96\xbf\xa5\x2f\x8f\x8f\x93\x41\xe7\x2c\xf4\xe2\xd5\x2a\xfe\xa0\xbf\x39\xa3\x9f\x64\xbb\xf5\x16\x37\xf3\x24\x5f\x55\x73\x9b\x50\xe1\x8b\x33\xfa\x73\x72\x7e\x54\x2c\x8e\xe2\x9d\xed\x5c\x86\xce\xaa\x94\x67\x90\x5b\x7c\x61\x06\x91\x04\x81\x9e\x0a\x7a\x3e\x59\xe5\x4b\x2a\xe
it's a long list.
Is there a tool to convert this into a readable text so that I can know the exact the content.
The two leading bytes "\x1f\x8b" indicate the data is gzip compressed.
You can use whatever gzip/gunzip library/utility you have to decode it.
Following is an example using Perl.
use IO::Uncompress::Gunzip;
my $in = "\x1f\x8b..."; # <- your string goes here
&IO::Uncompress::Gunzip::gunzip( \$in => \my $out );
print $out;
and it returns something like
!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){function c(a){var b=a.length,c=ab.type(a);return"function"===c||ab.isWindow(a)?!1:1===a.nodeType&&b?!0:"array"===c||0===b||"number"==typeof b&&b>0&&b-1 in a}function d(a,b,c){if(ab.isFunction(b))return ab.grep
Since the string in your question is truncated, above output is truncated.
I think I understand this correctly, but I just want to double check to be sure. Suppose I write a binary file using VBA where the first X number of bytes represents some field, the next x number another field, and so on. Now suppose I read that binary file back into VBA later using a byte array. Is it reasonable to assume that the first x elements in the byte array directly correlate to the first x bytes in the file?
I should have made this clear from the get-go, the format and header of the file isn't all that important, I'm just trying to get more into the nitty-gritty of reading and writing binary files and using byte arrays with vba. I'm getting there, and I appreciate everyone's input.
When writing binary files, write headers to check if the given bytes are really matching your format. Take a look into a wave file with a hex editor (i use HxD) and you will see something like this:
RIFFŽ...WAVEfmt ........D¬...±......dataà€....
RIFF is the header of the container (resource interchange
format)
followed by some bytes for meta information
WAVE is the
header of the actual wave
then some data follows you might want to
interprete
Here are two example binary read/write methods (converted to VB.NET from C#)
Public Shared Sub Write(This As YOUR_TYPE, stream As BinaryWriter)
stream.Write(FILE_IDENTIFIER)
stream.Write(FILE_VERSION)
While True
stream.Write(/*...*/)
End While
End Sub
Public Shared Function read(stream As BinaryReader) As YOUR_TYPE
If Not Enumerable.SequenceEqual(FILE_IDENTIFIER, stream.ReadBytes(FILE_IDENTIFIER.Length)) Then
Throw New FormatException("header mismatch")
End If
If stream.ReadByte() <> FILE_VERSION Then
Throw New NotSupportedException("version mismatch")
End If
Dim result As New YOUR_TYPE()
While True
stream.Read(/*...*/)
End While
Return result
End Function