Using Python I can easily split lines by str.splitlines().
But there is no splitline() attribute for MicroPython?
Because after my HTTP GET request I got this "b'\n1000\n999\n" and I want to seperate the two values (in this example "1000" and "999").
Any ideas? :/
str.splitlines() with MicroPython?
Re: str.splitlines() with MicroPython?
Code: Select all
[b'\n1000\n999\n'.split(b'\n')
Code: Select all
[b'', b'1000', b'999', b'']
Re: str.splitlines() with MicroPython?
[quote="Roberthh"][code][b'\n1000\n999\n'.split(b'\n')
[/code]reveals
[code][b'', b'1000', b'999', b'']
[/code][/quote]
Yup. As I understand it, micropython does not support UTF-8. Splitlines is a str method
[/code]reveals
[code][b'', b'1000', b'999', b'']
[/code][/quote]
Yup. As I understand it, micropython does not support UTF-8. Splitlines is a str method
Re: str.splitlines() with MicroPython?
Micropython does support utf8.
splitlines seems to work for me.
If you pass it a string you get strings back (and strings support UTF-8). If you pass it a byte string, you get byte strings back. bytestrings don't support UTF8.
To convert a bytestring to a string use: I'm not sure if the print statement will show up or not. It prints a> followed by a degree symbol followed by <b.
splitlines seems to work for me.
Code: Select all
>>> 'abc\ndef\nghi'.splitlines()
['abc', 'def', 'ghi']
>>> b'abc\ndef\nghi'.splitlines()
[b'abc', b'def', b'ghi']
To convert a bytestring to a string use:
Code: Select all
>>> str(b'a>\xc2\xb0<b', 'utf8')
'a>\xb0<b'
>>> bytes('a>\xb0<b', 'utf8')
b'a>\xc2\xb0<b'
>>> print(str(b'a>\xc2\xb0<b', 'utf8'))
a>°<b
>>>
Re: str.splitlines() with MicroPython?
Whether splitlines is supported depend on the platform. Splitlines works in the Unix and PyBoard version, but not in the esp8266 build. It depends on the setting of MICROPY_PY_BUILTINS_STR_SPLITLINES, which has to be enabled in the build configuration.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: str.splitlines() with MicroPython?
If you know that your separator is always '\n', there's no reason to use splitlines. Just use split('\n') instead.