Strings¶
flutils offers the following string utility functions:
as_escaped_unicode_literal
(text)[source]¶Convert the given
text
into a string of escaped Unicode hexadecimal.
- Parameters
text (
str
) – The string to convert.- Return type
A string with each character of the given
text
converted into an escaped Python literal.Example
>>> from flutils.strutils import as_escaped_unicode_literal >>> t = '1.★ 🛑' >>> as_literal(t) '\\x31\\x2e\\u2605\\x20\\U0001f6d1'
as_escaped_utf8_literal
(text)[source]¶Convert the given
text
into a string of escaped UTF8 hexadecimal.
- Parameters
text (
str
) – The string to convert.- Return type
A string with each character of the given
text
converted into an escaped UTF8 hexadecimal.Example
>>> from flutils.strutils import as_literal_utf8 >>> t = '1.★ 🛑' >>> as_escaped_utf8_literal(t) '\\x31\\x2e\\xe2\\x98\\x85\\x20\\xf0\\x9f\\x9b \\x91'
camel_to_underscore
(text)[source]¶Convert a camel-cased string to a string containing words separated with underscores.
Example
>>> from flutils.strutils import camel_to_underscore >>> camel_to_underscore('FooBar') 'foo_bar'
convert_escaped_unicode_literal
(text)[source]¶Convert any escaped Unicode literal hexadecimal character(s) to the proper character(s).
This function will convert a string, that may contain escaped Unicode literal hexadecimal characters, into a string with the proper characters.
- Parameters
text (
str
) – The string that may have escaped Unicode hexadecimal.- Return type
A string with each escaped Unicode hexadecimal character converted into the proper character.
The following Unicode literal formats are supported:
\x31 \u0031 \U00000031Examples
Basic usage:
>>> from flutils.strutils import convert_escaped_unicode_literal >>> a = '\\x31\\x2e\\u2605\\x20\\U0001f6d1' >>> convert_escaped_unicode_literal(a) '1.★ 🛑'This function is intended for cases when the value of an environment variable contains escaped Unicode literal characters that need to be converted to proper characters:
$ export TEST='\x31\x2e\u2605\x20\U0001f6d1' $ python>>> import os >>> from flutils.strutils import convert_escaped_unicode_literal >>> a = os.getenv('TEST') >>> a '\\x31\\x2e\\u2605\\x20\\U0001f6d1' >>> convert_escaped_unicode_literal(a) '1.★ 🛑'
convert_escaped_utf8_literal
(text)[source]¶Convert any escaped UTF-8 hexadecimal character bytes into the proper string characters(s).
This function will convert a string, that may contain escaped UTF-8 literal hexadecimal bytes, into a string with the proper characters.
- Parameters
text (
str
) – The string that may have escaped UTF8 hexadecimal.- Raises
UnicodeDecodeError – if any of the escaped hexadecimal characters are not proper UTF8 bytes.
- Return type
A string with each escaped UTF8 hexadecimal character converted into the proper character.
Examples
Basic usage:
>>> from flutils.strutils import convert_raw_utf8_escape >>> a = 'test\\xc2\\xa9' >>> convert_escaped_utf8_literal(a) 'test©'This function is intended for cases when the value of an environment variable contains escaped UTF-8 literal characters (bytes) that need to be converted to proper characters:
$ export TEST='test\\xc2\\xa9' $ python>>> import os >>> from flutils.strutils import convert_raw_utf8_escape >>> a = os.getenv('TEST') >>> a 'test\\xc2\\xa9' >>> convert_escaped_utf8_literal(a) 'test©'
underscore_to_camel
(text, lower_first=True)[source]¶Convert a string with words separated by underscores to a camel-cased string.
- Parameters
- Return type
Examples
>>> from flutils.strutils import underscore_to_camel >>> underscore_to_camel('foo_bar') 'fooBar' >>> underscore_to_camel('_one__two___',lower_first=False) 'OneTwo'