[Python-ideas] New explicit methods to trim strings
Rhodri James
rhodri at kynesim.co.uk
Tue Apr 2 14:02:09 EDT 2019
On 02/04/2019 18:55, Stephen J. Turnbull wrote:
>> = Me
> > 3. My most common use case (not very common at that) is for stripping
> > annoying prompts off text-based APIs. I'm happy using
> > .startswith() and string slicing for that, though your point about
> > the repeated use of the string to be stripped off (or worse,
> > hard-coding its length) is well made.
>
> I don't understand this use case, specifically the opposition to
> hard-coding the length. Although hard-coding the length wouldn't
> occur to me in many cases, since I'd use
>
> # remove my bash prompt
> prompt_re = re.compile(r'^[^\u0000-\u001f\u007f]+ \d\d:\d\d\$ ')
> lines = [prompt_re.sub('', line) for line in lines]
For me it's more often like
input = get_line_from_UART()
if input.startswith("INFO>"):
input = input[5:]
do_something_useful(input)
which is error-prone when you cut and paste for a different prompt
elsewhere and forget to change the slice to match.
--
Rhodri James *-* Kynesim Ltd
More information about the Python-ideas
mailing list