A
A
Anton2018-08-23 07:27:40
Python
Anton, 2018-08-23 07:27:40

How to copy words between certain characters?

I need to run a command on every mail server folder. In issue received the list of folders. As far as I figured out, the folder name is between the symbols "/" " and "'
But somehow I can’t seem to get these names into a list :(
I tried to shaman with the re module, something doesn’t work :(

import re

string = """('OK', [b'(\\Inbox) "/" "INBOX"', b'() "/" "&BCIENQRBBEIEPgQyBEsENQ- &BEIENQQ7BDUEMwRABDAEPAQ8BEs-"', b'(\\Spam) "/" "&BCEEPwQwBDw-"', b'(\\Sent) "/" "&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-"', b'(\\Drafts) "/" "&BCcENQRABD0EPgQyBDgEOgQ4-"', b'(\\Trash) "/" "&BBoEPgRABDcEOAQ9BDA-"', b'(\\NoInferiors) "/" "INBOX/Social"', b'(\\NoInferiors) "/" "INBOX/News Letters"'])"""

string = re.findall(r'"/" "+\D+"\'', string)

print(string)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-08-23
@8toni8

Why re? It is not necessary to import a special module - I think it is possible to assemble such a list with standard splits or particip ...
With the help of partition, you can quickly do this:

string = """('OK', [b'(\\Inbox) "/" "INBOX"', b'() "/" "&BCIENQRBBEIEPgQyBEsENQ- &BEIENQQ7BDUEMwRABDAEPAQ8BEs-"', b'(\\Spam) "/" "&BCEEPwQwBDw-"', b'(\\Sent) "/" "&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-"', b'(\\Drafts) "/" "&BCcENQRABD0EPgQyBDgEOgQ4-"', b'(\\Trash) "/" "&BBoEPgRABDcEOAQ9BDA-"', b'(\\NoInferiors) "/" "INBOX/Social"', b'(\\NoInferiors) "/" "INBOX/News Letters"'])"""

res = []
st_rest = string.partition('''/" "''')[2]

while st_rest != '':
    part = st_rest.partition(""""'""")
    res.append(part[0])
    st_rest = part[2].partition('''/" "''')[2]

for r in res: print(r)

# INBOX
# &BCIENQRBBEIEPgQyBEsENQ- &BEIENQQ7BDUEMwRABDAEPAQ8BEs-
# &BCEEPwQwBDw-
# &BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-
# &BCcENQRABD0EPgQyBDgEOgQ4-
# &BBoEPgRABDcEOAQ9BDA-
# INBOX/Social
# INBOX/News Letters
... you can probably write more sensibly, but, as for me, there is no problem at all.
PS: and yes, I expanded the symbols framing the names of the folders a little (I supplemented those suggested in the question)

N
NetBear, 2015-02-14
@kirill_grosul

Because len() returns an int and you need a str because the write() method can only work with the latter.
Replace the penultimate line with:
output.write(str(s))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question