How to split String to int and bytestring literal - django

I have the following string:
str = "3, b'\\xf3\\xc71\\xe9\\xad_\\xce\\x8bI\\x1c\\x04Y\\xd5z\\xa2Q'"
I need to split it in order to get two variables, an int and a bytestring like so:
number = 3
bytestring = b'\\xf3\\xc71\\xe9\\xad_\\xce\\x8bI\\x1c\\x04Y\\xd5z\\xa2Q'
What I tried doing:
number, bytestring = [s for s in str.split(", ")]
int_number = int(number)
bytestring_in_bytes = bytestring.encode()
This unfortunately didn't work for the bytestring and I ended up with something like this:
bytesring_in_bytes = b"b'\\xf3\\xc71\\xe9\\xad_\\xce\\x8bI\\x1c\\x04Y\\xd5z\\xa2Q'"
Any idea how to get the bytestring from the string?

What you here have seems like the textual representation (in Python the repr(..)) of a bytestring.
You can use ast.literal_eval(..) to convert this to a bytestring:
from ast import literal_eval
bytestring_in_bytes = literal_eval(bytestring)
Note that in case it contains a string, int, etc., then the type of bytestring_in_bytes will be a str, int, etc. as well.

Related

How do you convert a character to a real in SML?

If I'm trying to do this:
Convert #”N” to a real
What ML expression would do this?
In the same way that:\
str = Char -> string
chr = int -> Char
ord = Char -> int
real = int -> real
Is there an ML expression like the ones stated above that can convert char -> real?
The input would be something like:
real #"N";
and the output would be:
val it = "whatever the value is": real
No you would need to use two functions:
real(ord(#"N"));
Breakdown:
ord(#"N"); ...... converts to ascii value, 78.
real(78); .......... converts int to real, 78.0.

How to convert UTF→CP1251 and finally to URL-encoded %CA%F3%EF%E8%F2%FC+%EA%E2%E0%F0%F2%E8%F0%F3

I have a phrase in Russian "Купить квартиру". I need to convert it to
%CA%F3%EF%E8%F2%FC+%EA%E2%E0%F0%F2%E8%F0%F3
Encoding looks like ANSI
Notice, if I Uri.EscapeDataString("Купить квартиру"), I get
%D0%9A%D1%83%D0%BF%D0%B8%D1%82%D1%8C%20%D0%BA%D0%B2%D0%B0%D1%80%D1%82%D0%B8%D1%80%D1%83
But these strings are not equal.
Is there some correct way to convert?
Uri.EscapeDataString follows the URL spec RFC 3986, which says to use UTF-8 character encoding.
You'll need to write your own function in custom M, like this:
let
To1251URL = (input as text) as text => let
ToBytes = Binary.ToList(Text.ToBinary(input, 1251)),
ToText = Text.Combine(List.Transform(ToBytes, (b) => "%" & Number.ToText(b, "X"))),
FixSpace = Text.Replace(ToText, "%20", "+")
in
FixSpace,
Applied = To1251URL("Купить квартиру")
in
Applied

C++ Substract the end of a string, not knowing length of the result

I have a string like this: 001,"John Marvin","doctor", "full time"
I want to delete everything after (001) with substr, but, the length of (001) is not always 3 so I can not put something like thie:
string chain = "001,\"John Marvin\",\"doctor\", \"full time\"";
std::string partial = chain.substr(0,3);
How can I proceed in this case?
You could find the index of the first comma and use that to determine where to cut off the string.
Something like:
string chain = "001,\"John Marvin\",\"doctor\", \"full time\"";
int cutoff = chain.find(',');
string newString = chain.substr(0, cutoff);
Tested here.

Detect and transform numbers in a string using regular expressions

How can I use a regular expression and matching to replace contents of a string? In particular I want to detect integer numbers and increment them. Like so:
val y = "There is number 2 here"
val p = "\\d+".r
def inc(x: String, c: Int): String = ???
assert(inc(y, 1) == "There is number 3 here")
Using replaceAllIn with a replacement function is one convenient way to write this:
val y = "There is number 2 here"
val p = "-?\\d+".r
import scala.util.matching.Regex.Match
def replacer(c: Int): Match => String = {
case Match(i) => (i.toInt + c).toString
}
def inc(x: String, c: Int): String = p.replaceAllIn(x, replacer(c))
And then:
scala> inc(y, 1)
res0: String = There is number 3 here
Scala's Regex provides a handful of useful tools like this, including a replaceSomeIn that takes a partial function, etc.

Scala convert a string to raw

I know raw String can be declared as:
val foo: String = """foo"""
or
val foo: String = raw"foo"
However, if I have a string type val, how can I convert it to raw? For example:
// val toBeMatched = "line1: foobarfoo\nline2: lol"
def regexFoo(toBeMatched: String) = {
val pattern = "^.*foo[\\w+]foo.*$".r
val pattern(res) = toBeMatched /* <-- this line induces an exception
since Scala translates '\n' in string 'toBeMatched'. I want to convert
toBeMatched to raw string before pattern matching */
}
In your simple case, you can do this:
val a = "this\nthat"
a.replace("\n", "\\n") // this\nthat
For a more general solution, use StringEscapeUtils.escapeJava in Apache commons.
import org.apache.commons.lang3.StringEscapeUtils
StringEscapeUtils.escapeJava("this\nthat") // this\nthat
Note: your code doesn't actually make any sense. Aside from the fact that String toBeMatched is invalid Scala syntax, your regex pattern is set up such that it will only match the string "foo", not "foo\n" or "foo\\n", and pattern(res) only makes sense if your regex is attempting to capture something, which it's not.
Maybe (?!) you meant something like this?:
def regexFoo(toBeMatched: String) = {
val pattern = """foo(.*)""".r
val pattern(res) = toBeMatched.replace("\n", "\\n")
}
regexFoo("foo\n") // "\\n"